DRACULA1729 commented on issue #68398:
URL: https://github.com/apache/airflow/issues/68398#issuecomment-4703078315

   Took a look at this one and I think I found it.
   
   The KubernetesExecutor grew a helper called 
`_alive_other_scheduler_job_ids()` (added in #66400, shipped in a recent 
cncf-kubernetes provider release). It opens its own `with create_session() as 
session:` block, but by the time it runs the scheduler is already inside the 
`adopt_or_reset_orphaned_tasks` transaction.
   
   The catch: `settings.Session` is a thread-local `scoped_session`, so that 
nested `create_session()` hands back the *same* session the scheduler is 
already using. When the helper's `with` block exits it calls `session.close()`, 
which detaches every TaskInstance the scheduler just loaded. The next 
`repr(ti)` then triggers a deferred load of `ti.state` on a detached instance, 
and that's your `DetachedInstanceError`.
   
   Fits both things you saw: it only bites under the KubernetesExecutor, and 
the `UPDATE ... SET state='failed'` makes it go away because there are no 
orphaned tasks left to reset, so the loop calling `repr(ti)` never runs.
   
   Fix is to give that helper its own independent (non-scoped) session so 
closing it can't pull the rug out from under the caller. PR coming shortly.
   
   ---
   Drafted-by: Claude Code (Opus 4.8) (no human review before posting)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to