Repository navigation
Conversation
The closed check is repeated on the session thread, so a snippet dispatched after close() fails with IllegalStateException instead of running on a disposed compiler, or being cancelled by the rejecting dispatcher of asCoroutineDispatcher. close() uses compareAndSet, so concurrent calls cannot submit to a shut down thread. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Code reviewNo issues found. Checked for bugs and CLAUDE.md compliance. |
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.
Fixes #141
Fix
Both races between
close()andexecute()are closed:closedis anAtomicBoolean, andclose()usescompareAndSet, so only one call cancels, queues the disposal and shuts the thread down — a concurrent secondclose()has no effect, instead of throwingRejectedExecutionException;executechecksclosedagain on the session thread, insidewithContext, before touching the compiler — so a snippet passing it was queued before the disposal, which always remains the last task on the thread;asCoroutineDispatcher()is replaced by a small dispatcher which hands a rejected block toDispatchers.IOwithout cancelling it, where its only effect is to fail that check withIllegalStateException("The session is closed")— before, a rejected snippet failed with
CancellationException: The task was rejected, as if the caller was cancelled, and one queued after the disposal withIncorrectOperationException: … has already been disposed.The KDoc of
execute(@throws IllegalStateException, also for a session closed while the snippet waits for the ones running concurrently) andclose()describe it.DefaultScriptSessiontakes a new internalexecutorconstructor parameter, a test seam to make the interleavings deterministic; the publicScriptSession(…)factory does not expose it, and the ABI dump is unchanged.Tests
should reject snippet of session closed before dispatching it to the thread of the session— closes the session from within the dispatch, so it is rejected;should reject snippet of session closed before dispatching it to the thread of the session after the disposal— the snippet, past the early check, is queued behind the disposal;both deterministic, failing before the fix (the reviewer re-verified it by removing the on-thread check);
should close session concurrently from many threads— a stress guard only: the race window is a single volatile read, so it did not fail before the fix either (see Harden the closing of a session: deterministic test, dispatcher fallback #162).Review
One round of
/code-review high, no blocking findings: the ordering of the CAS, the disposal and the shutdown was verified, and the custom dispatcher keeps what kotlinx.coroutines expects (isDispatchNeeded, the resumption on the caller's dispatcher).Follow-ups filed
close(), theDispatchers.IOfallback catching (hypothetical) resumptions, the dispatcher'stoString().🤖 Generated with Claude Code