Skip to content

fix(aistio): deregister on close without a heartbeat registering the instance again (#3132) - #4542

Open
aiyili wants to merge 5 commits into
agentscope-ai:2.0.xfrom
aiyili:fix/aistio-unregister-on-close-20x
Open

aiyili wants to merge 5 commits into
agentscope-ai:2.0.xfrom
aiyili:fix/aistio-unregister-on-close-20x

Conversation

@aiyili

@aiyili aiyili commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #3132 on 2.0.x, the only branch where agentscope-extensions-aistio still exists; the module is gone from main, so #3131 can no longer carry the fix.

Problem

HttpSelfRegistration.close() stops the heartbeat thread with shutdownNow() and deregisters the instance afterwards. Two things go wrong when a heartbeat is on the wire at that moment:

Repeated 300 times on unmodified 2.0.x in a Linux container, with the test's 20 ms heartbeat: 291 runs registered the instance a second time, 277 of them after the DELETE; a batch of 300 before that hit the skipped DELETE once. In CI it shows as a flaky HttpSelfRegistrationTest on Windows, on any PR against 2.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 lock close() uses to set it. A registration that was still on the wire when close() ran therefore never publishes into the closed instance; it sends the control plane a DELETE of 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 the registered flag. 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 until close() has run. Both fail on the previous code, in 9 runs out of 9:

  • aHeartbeatInterruptedByCloseDoesNotRegisterTheInstanceAgain: the server keeps a heartbeat's response until close() has returned, so the interrupt lands while the request is on the wire. Exactly one registration and one DELETE must 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, so registered is clear while the instance id is set. close() during that must still DELETE; before, nothing was sent. The repeated close in the finally block 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 one DELETE reaches the server, nothing is retried, no identity is published, and start() survives the race without an exception.
  • aFailedWithdrawDoesNotDisturbTheShutdown: the withdraw's DELETE fails at the transport level, and the shutdown stays clean.

Two more pass before and after and guard what must not change:

  • startAfterCloseRegistersTheInstanceAgain: the block that start() lifts.
  • closeOfAnInstanceThatNeverRegisteredSendsNothing: a registration the control plane rejects is not deregistered by close().

Each piece of the fix was then broken on its own:

Broken piece Caught by
tryRegister() does not check closed at entry aHeartbeatInterruptedByCloseDoesNotRegisterTheInstanceAgain: without it, the interrupted heartbeat registers and withdraws, and the test counts two registrations
close() returns early when registered is clear closeDeregistersWhileAFailedHeartbeatIsReRegistering
the publish step does not re-check closed aRegistrationInFlightDuringCloseWithdrawsItself: the late registration is published and never withdrawn
start() schedules heartbeats although a close raced the registration the two in-flight tests: start() dies with a RejectedExecutionException
start() does not lift the block startAfterCloseRegistersTheInstanceAgain
close() deregisters without a registered id closeOfAnInstanceThatNeverRegisteredSendsNothing, and the repeated close of closeDeregistersWhileAFailedHeartbeatIsReRegistering

All 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-aistio suite 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 found in the image) and MarketplaceStagerOrphanGcTest.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 skipped DELETE.

CI is green on both systems on the current head. One Windows run before it stopped on HarnessAgentOverflowRecoveryTest in agentscope-harness, which this change does not touch: a 5 s block timed out on the runner, and the next run passed.

…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.
@codecov

codecov Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

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 juruoO-o left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@aiyili

aiyili commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

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: close() read the old id (or null), returned, and the ghost stayed registered with nobody left to deregister it.

Both halves of your suggestion are in:

  • Coordinate: the publish step of tryRegister() re-checks the flag under a lifecycle lock — the same lock close() uses to set it. A registration either publishes before that lock (and close() then deregisters it by id, since it reads the id afterwards) or sees the flag and never publishes. The lock guards a handful of field writes and is never held during I/O, so close() still returns without waiting for a response.
  • Compensate: a registration that sees the flag withdraws what the control plane just created — a DELETE of its own durable id and generation, best-effort, with nothing published (no id, no identity, no listener call).

The deterministic test you asked for is aRegistrationInFlightDuringCloseWithdrawsItself: the server holds the 201 response while close() runs on another thread and asserts the final membership — close() sends nothing while nothing is published, then exactly one DELETE arrives, nothing is retried, no identity was published, and start() survives the race without an exception. aFailedWithdrawDoesNotDisturbTheShutdown covers the same shape with the withdraw's DELETE failing at the transport level. Removing the publish-time re-check fails the first test; the entry check is also still pinned — without it the interrupted heartbeat of the existing test registers-and-withdraws, and its registration count assertion fails.

One adjacent hardening that fell out of the interleaving: start() now 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.

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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants