Skip to content

[Bug]: Session writer lease is not released when a call is cancelled during acquisition #4070

Closed

Description

@ningmao-hlyz

Summary

Cancelling a call while its session is being opened leaves the writer lease taken. A reconnect
with the same userId/sessionId is then rejected with
SessionLogException: Session already has an active writer until the lease expires
(SessionRecorder.LEASE, two minutes). In four of five runs the writer was still held 70 s
after the cancellation.

Mono.usingWhen only releases a resource it has already received. ReActAgent.acquireCallExecution
creates the SessionRecorder — which takes the writer lease in its constructor — and then starts
the journal, both before the resource is emitted. A cancellation in either step has nothing to
release, and it is invisible to doOnDiscard because no value was ever produced.

The effect in a web deployment: a client that drops mid-turn and re-asks immediately gets an
error. The journal itself is not damaged — the history is intact once the lease expires — but the
session is unusable until then.

Cancelling later, while the model streams, is handled: the usingWhen cancellation handler runs
releaseCallExecution(scope, "cancelled"). That cleanup is asynchronous, so a retry issued in the
same millisecond can still collide, but it does not wait for the lease.

To Reproduce

var store = new InMemorySessionLogStore();
var parked = new CountDownLatch(1);
// Open the real journal, then park inside acquisition so the test controls the window.
SessionLogStore gated = (key, ctx) -> {
    SessionLog log = store.open(key, ctx);
    try {
        parked.await();
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new IllegalStateException("parked open interrupted", e);
    }
    return log;
};

var agent = ReActAgent.builder().name("stable").model(hangingModel()).sessionLogStore(gated).build();
Disposable d = agent.call(List.of(new UserMessage("hello")), context()).subscribe(...);
// ... wait until the store has been opened, then:
d.dispose();
parked.countDown();

// Same userId/sessionId:
ReActAgent.builder().name("stable").model(answerModel()).sessionLogStore(store).build()
        .call(List.of(new UserMessage("continue")), context())
        .block();
// → SessionLogException: Session already has an active writer

Cancelling a few tens of milliseconds later, while the first journal write is in flight, lands in
the second window and shows the same error.

Expected: the next call acquires the session writer.

Actual: it is rejected until the lease expires.

Suggested fix

Record the cancellation while acquiring, and close the recorder through the existing failure path
once it exists; do the same when the cancellation arrives while the journal is being started. The
lease then comes back as soon as the cancelled call unwinds instead of at lease expiry. Nothing
else about the call changes — the record of a turn cancelled in that first write may be
incomplete, which is the same trade the client made by disconnecting.

Activity

  1. added a commit that references this issue on Nov 8, 2026
    f41c0a7
  2. added a commit that references this issue on Nov 10, 2026
    2b4a75a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions