Repository navigation
Conversation
…instance again Fixes agentscope-ai#3132 on 2.0.x, the only branch where agentscope-extensions-aistio exists. `HttpSelfRegistration.close()` stops the heartbeat thread with `shutdownNow()`. A heartbeat that is on the wire at that moment fails, and `heartbeatSafe()` takes that for a control-plane failure: it clears `registered` and registers the instance again, during its own shutdown. When that registration reaches the control plane after the DELETE, the instance is registered again after it was deregistered. And when the heartbeat had failed shortly before, `registered` is already clear when `close()` reads it, so no DELETE is sent at all. `tryRegister()` now does nothing once `close()` has started; `start()` lifts the block, so a registration can still be started again after it was closed. `close()` deregisters by the registered instance id instead of the `registered` flag. Two cases in `HttpSelfRegistrationTest` pin the interleavings with a server that holds a response until `close()` has run; both fail on the previous code. Repeating the scenario 300 times: 291 second registrations before, 277 of them after the DELETE; none with the change.
…st, which this change does not touch
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
… closes `close()` checked the registered id for blank as well. That case cannot occur: `tryRegister()` stores an id only once the response passed its own blank check. The check is dropped, and codecov no longer reports the branch as partial. Two assertions pin what the description claims and no test did: `close()` of an instance that never registered sends nothing, and so does a repeated close. The server records every request that matches no route for it. Both pass before this commit and after.
juruoO-o
left a comment
There was a problem hiding this comment.
The closed check only protects entry to tryRegister(). A registration already in flight can complete after close() reads the old instance ID (or null) and returns. Please coordinate shutdown with the in-flight registration or compensate for a late successful registration. A deterministic test should delay the registration response across close() and assert the final control-plane membership.
`tryRegister()` checked the closed flag only at entry, so a registration already on the wire completed after `close()` had read the instance id (or null) and returned: the instance ended up registered with nobody left to deregister it. The publish step now re-checks the flag under the lock `close()` uses to set it, and a registration that sees it withdraws what the control plane just created with a DELETE of its own. `close()` never holds the lock during I/O, so it still returns without waiting for the response. `start()` captures its executor in a local and skips the heartbeat schedule when a close raced it, instead of scheduling on a shut-down executor or dereferencing the nulled field. Two tests hold the registration response across `close()` on another thread and assert the final control-plane membership: the late registration withdraws itself (exactly one DELETE, no retry, no published identity, `start()` survives the race), and a withdraw that fails at the transport level does not disturb the shutdown.
|
Thanks for the review — you're right, the entry check only protected the door, and a registration already on the wire completed into a closed instance: Both halves of your suggestion are in:
The deterministic test you asked for is One adjacent hardening that fell out of the interleaving: Test class is 7/7 green, 10 runs in a row; the module suite went from 74 to 80 with no new failures. |
…4.29.3 from central The protobuf compile phase could not resolve the protoc artifact on the runner, before any test ran. Unrelated to the change.
Fixes #3132 on
2.0.x, the only branch whereagentscope-extensions-aistiostill exists; the module is gone frommain, so #3131 can no longer carry the fix.Problem
HttpSelfRegistration.close()stops the heartbeat thread withshutdownNow()and deregisters the instance afterwards. Two things go wrong when a heartbeat is on the wire at that moment:heartbeatSafe()takes that for a control-plane failure: it clearsregisteredand callstryRegister(), which registers the instance again, during its own shutdown. When that registration reaches the control plane after theDELETE, the instance is registered again after it was deregistered.close()decides whether to deregister by readingregistered. A heartbeat that failed shortly before has cleared that flag, soclose()returns without aDELETEand the instance stays registered: the leak of [Bug]: HttpSelfRegistration.close() skips deregistration when a heartbeat re-register cycle is in flight (flaky HttpSelfRegistrationTest on CI) #3132.Repeated 300 times on unmodified
2.0.xin a Linux container, with the test's 20 ms heartbeat: 291 runs registered the instance a second time, 277 of them after theDELETE; a batch of 300 before that hit the skippedDELETEonce. In CI it shows as a flakyHttpSelfRegistrationTeston Windows, on any PR against2.0.x; the run that failed for me is linked in the issue.Fix
tryRegister()checks the closed flag twice: at entry, and again in the publish step under the lockclose()uses to set it. A registration that was still on the wire whenclose()ran therefore never publishes into the closed instance; it sends the control plane aDELETEof its own and publishes nothing — no id, no identity, no listener call.close()sets the flag under the same lock and then deregisters by the registered instance id, not by theregisteredflag. It never holds the lock during I/O, so it still returns without waiting for a response. Never-registered and repeated closes stay no-ops; the id is null until a registration succeeded and after a close, and the blank check it used to make on it is dropped.start()keeps its executor in a local and skips the heartbeat schedule when a close raced the initial registration, instead of scheduling on a shut-down executor or dereferencing the nulled field.Tests
HttpSelfRegistrationTest, 7 cases. Two pin the interleavings of the issue with a local server that holds a response untilclose()has run. Both fail on the previous code, in 9 runs out of 9:aHeartbeatInterruptedByCloseDoesNotRegisterTheInstanceAgain: the server keeps a heartbeat's response untilclose()has returned, so the interrupt lands while the request is on the wire. Exactly one registration and oneDELETEmust reach the server; before, a second registration did.closeDeregistersWhileAFailedHeartbeatIsReRegistering: a heartbeat is dropped without a response, and the server holds the response of the re-registration that follows, soregisteredis clear while the instance id is set.close()during that must stillDELETE; before, nothing was sent. The repeated close in thefinallyblock must send nothing, which the server checks by recording every request that matches no route.Two more pin the review interleaving, a registration whose response is held across a
close()on another thread:aRegistrationInFlightDuringCloseWithdrawsItself:close()returns while the response is still held and sends nothing; when it is released, the late registration withdraws itself — exactly oneDELETEreaches the server, nothing is retried, no identity is published, andstart()survives the race without an exception.aFailedWithdrawDoesNotDisturbTheShutdown: the withdraw'sDELETEfails at the transport level, and the shutdown stays clean.Two more pass before and after and guard what must not change:
startAfterCloseRegistersTheInstanceAgain: the block thatstart()lifts.closeOfAnInstanceThatNeverRegisteredSendsNothing: a registration the control plane rejects is not deregistered byclose().Each piece of the fix was then broken on its own:
tryRegister()does not checkclosedat entryaHeartbeatInterruptedByCloseDoesNotRegisterTheInstanceAgain: without it, the interrupted heartbeat registers and withdraws, and the test counts two registrationsclose()returns early whenregisteredis clearcloseDeregistersWhileAFailedHeartbeatIsReRegisteringclosedaRegistrationInFlightDuringCloseWithdrawsItself: the late registration is published and never withdrawnstart()schedules heartbeats although a close raced the registrationstart()dies with aRejectedExecutionExceptionstart()does not lift the blockstartAfterCloseRegistersTheInstanceAgainclose()deregisters without a registered idcloseOfAnInstanceThatNeverRegisteredSendsNothing, and the repeated close ofcloseDeregistersWhileAFailedHeartbeatIsReRegisteringAll six are caught, each by the case written for it. Every line this PR changes in production code is covered on Linux, branches included.
Verification
The full
agentscope-extensions-aistiosuite was run before and after the change in a Linux container (maven:3.9-eclipse-temurin-17): 74 tests before, 80 after, no failure on either side. The modules it is built with in the same run show the same four environmental failures before and after, outside this change: 3 ×BaseSandboxFilesystemTest.edit_*(python3: not foundin the image) andMarketplaceStagerOrphanGcTest.unreadableEntryDoesNotFailStage. The test class passed 10 runs in a row on the final code; the scenario of the original test, repeated 300 times: no second registration, no skippedDELETE.CI is green on both systems on the current head. One Windows run before it stopped on
HarnessAgentOverflowRecoveryTestinagentscope-harness, which this change does not touch: a 5 sblocktimed out on the runner, and the next run passed.