Repository navigation
Conversation
…ction ExtendedAsyncSAEngine.advisory_lock() takes a session-level lock on a connection that SQLAlchemy autobegan at SERIALIZABLE, so idle_in_transaction_session_timeout (60 s) killed it and released the lock while the holder still ran. The lock connection is now AUTOCOMMIT.
2 of 7 tasks
Member
Author
|
Closed: superseded by #15978 (start claim); not needed for the CC MVP. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
resolves #NNN (BA-XXXX)
ExtendedAsyncSAEngine.advisory_lock()now takes its session-levelpg_advisory_lockon an AUTOCOMMIT connection, so the lock holder is never "idle in transaction" and PostgreSQL'sidle_in_transaction_session_timeoutcan no longer drop a held lock.Problem
pgsql_connect_optssetsidle_in_transaction_session_timeout=60000on every connection (models/base.py). A locked body longer than 60 s gets its connection terminated by PostgreSQL, which releases the session-level lock while the holder is still running.lock connection already closed, unlock skipped.Change (
src/ai/backend/manager/models/utils.py)lock_conn = await lock_conn.execution_options(isolation_level="AUTOCOMMIT")before the first statement. The same idiom is already used on pooled connections for READ COMMITTED inbegin_session_read_committed()andbegin_readonly_session_read_committed().Does AUTOCOMMIT leak back into the pool? No. Shown from SQLAlchemy 2.0.54 source:
DefaultDialect.set_connection_execution_options(engine/default.py~L694) appends_reset_characteristicsto_connection_record.finalize_callbackwhen an execution option is applied._ConnectionRecord.checkin(pool/base.py~L765) pops and runs everyfinalize_callbackbefore the connection returns to the pool.IsolationLevelCharacteristic.reset_characteristic(engine/characteristics.py~L100) callsdialect.reset_isolation_level, which restores the engine-level isolation (SERIALIZABLE here) (engine/default.py~L1004).test_lock_connection_isolation_level_is_restoredchecks that the next pooled connection reports SERIALIZABLE.Rig evidence (a TDX-guest session start on a CoCo test rig;
create_kernelsRPC in flight ~60 s)lifecycle schedule processingand takes the locklifecycle schedule processing~13 s later; the START one blocks on the locklock connection already closed, unlock skippedsession start attemptedfor the same session, with nolifecycle schedule processingof its own, i.e. the waiter from lines 2601-2624create_kernelsis dispatched a second timeShown vs inferred:
Test (
tests/unit/manager/models/test_advisory_lock.py, real PostgreSQL)test_lock_survives_idle_in_transaction_timeout: dedicated engine withidle_in_transaction_session_timeout=1000, holdsadvisory_lockfor 2 s, probes from another connection withpg_try_advisory_xact_lock. Before the change:assert True is False(lock was gone). After: passes.test_lock_connection_isolation_level_is_restored: regression guard for the pool reset; it also passes on main.test_lock,test_lock_timeout,test_dbutils.py,test_extended_async_sa_engine.pypass.Side effect: a waiter now hits
lock_timeout. A pass queued longer thanlock_timeout=60000for a lock gets 55P03, whichadvisory_lockconverts toasyncio.CancelledError(models/utils.pyL310-312). Before this change the holder's lock was dropped at 60 s, so such a waiter ran concurrently; now it is skipped. What happens to the skipped pass:DoSokovanProcessIfNeededEvent/DoSokovanProcessScheduleEventconsumer task (event_dispatcher/dispatch.pyL430-441 ->handlers/schedule.pyL80-91 ->coordinator.pyprocess_if_neededL1516-1528 ->process_lifecycle_schedule->_process_lifecycle_handler_scheduleL293, lock at L304-312).CancelledErroris aBaseException:_handle_decodedre-raises it (common/events/dispatcher.pyL589-596),_handleonly catchesException(L511). The task ends as cancelled;PersistentTaskGroup._on_task_donereturns early for a cancelled task (aiotoolstaskgroup/persistent.pyL222-224), so the consumer group is not shut down. Checked with a script: after a child raisingCancelledError,task.cancelled()is True, the nextcreate_taskruns,_base_erroris None.GlobalTimer._generate_tick(common/distributed.pyL50-66) only produces the event; the handler runs in a separate consumer task.process_if_neededcallsload_and_delete_schedule_mark(coordinator.pyL1525), the lock comes at L304-312. So the 2 sprocess_if_neededtick does not pick up a pass skipped by 55P03. The session is recovered by the unconditional 60 sprocess_scheduletick (START spec,long_interval=60.0,coordinator.pyL1658-1662,sokovan.pyL68-87) or by a new START mark (a transition to PREPARED marks START:post_processors/schedule_marking.pyL28). A session still PREPARED is then selected by the START pass (handlers/lifecycle/start_sessions.pyL66-76).ExceptionandBaseExceptionbranches of_handle_decoded(common/events/dispatcher.pyL577-596) return or raise beforepost_callback.done(), so a failed or cancelled handler never acks. main can already reach 55P03 by the same path; this PR only makes a wait longer than 60 s more likely, because PostgreSQL no longer kills the holder.message_queue/redis_queue/consumer.pyL29,_auto_claimL357-400). The replay is one more START pass serialised behind a lock that is now really held. A replayedprocess_if_neededfinds no mark and returns False.asyncio.timeout(lock_conn_timeout)wraps the whole locked body, and the 55P03 mapping also catches a 55P03 raised inside the body.Not in this change
advisory_lockinappproxy/coordinator/models/utils.pyL205-225,idle_in_transaction_session_timeout=60000inappproxy/coordinator/models/base.pyL67, SERIALIZABLE by default) but no current caller of the lock on main b0e8cd0 (the AUTOCOMMIT change should be applied there if the lock gets a caller).Checklist: (if applicable)
fix:PR goes to every version in.github/maintained-versions.ymlautomatically.A
Backport:line here names the targets instead — some of them, other ones, ornone.ai.backend.testdocsdirectory