Repository navigation
Description
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 writerCancelling 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
fix(core): release the session writer when a call is cancelled during…
fix(core): release the session writer when a call is cancelled during…