Repository navigation
Cancel interrupted queued calls, document when close() returns early - #71
Merged
Merged
Conversation
…early (#29) Closes #29 by narrowing it: its first bullet, a second close() waiting for the interpreter to be closed, is not fixed, but documented instead. As decided, the design of #32 stays: close() returns without waiting whenever something runs on the Python thread, which includes the closing itself, as closing the interpreter runs Python code (__del__) which can wait for a close() on another thread. So only the first close() can wait, and a later one returns at once, also while the interpreter is still being closed. The KDoc of close() and the README say so. Fixed, from the abandoned branch issue-29-close-lifecycle: - a call whose caller is interrupted while waiting for its turn is cancelled with future.cancel(false), and does not run, unless the Python thread has already started it; - close() and the calls restore the interrupt flag when they throw an InterruptedException, as Java code cannot catch it undeclared; - the interpreter reference is released also when closing it fails. Documented with regression tests, already the behavior of main: a failure of closing the interpreter is thrown by the close() waiting for it, and not again by a later close(), so that use or try-with-resources around an explicit close() reports it once; a close() reached from __del__ during closing, directly or from another thread, does not deadlock. Python.withInterpreterFactory is a new internal test seam, to make closing the interpreter fail. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FJmHQ3jHRbVdpwUiNUkApG
) Polish after the review of the fix of #29: - an InterruptedException thrown by a call or close() no longer sets the interrupt flag again, as it made the next blocking operation of the caller, e.g. the close() of use { }, fail at once; the public functions which can throw it declare it with @throws, so that Java callers can catch it; - the README tells that an interrupted close() throws at once, while the interpreter is still closed later, in the background; - the comment on releasing the interpreter in finally says it is defensive only; - the default interpreter factory is a default parameter value. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FJmHQ3jHRbVdpwUiNUkApG
morisil
added a commit
that referenced
this pull request
Nov 13, 2026
#71, which this branch was stacked on, was squash-merged into main as f7b4a2e. Main holds the same content as #71's branch, so the conflicts (README.md, Python.kt, PythonCloseTest.kt) take this branch's side, leaving only the changes of #86 on top of main. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FJmHQ3jHRbVdpwUiNUkApG
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 #29 — by narrowing it, as decided after the earlier run's design conflict with #32.
Kept and documented (first bullet): #32's design stays —
close()returns without waiting while a call or the closing itself runs on the Python thread, to avoid the callback deadlock.Only the first
close()can wait; a later one returns at once, also while the interpreter is still being closed, after an interrupted firstclose(), or when the first returned early. KDoc and README say so.Fixed (second bullet): a caller interrupted while its call waits for its turn now cancels it (
future.cancel(false)), so it never runs unless the Python thread already started it (then it completes, unreported — documented).Also:
InterruptedExceptionis thrown with the standard contract (interrupt flag cleared) and declared with@Throwsonexec,invoke,eval,get,setandclose, so Java callers can catch it.close()waiting for it; laterclose()calls return quietly (try-with-resources /usereport it once). Main's behavior, now documented and tested.__del__reaches a Kotlin callback callingclose(), directly or from another thread.Python.withInterpreterFactory(private primary constructor kept, per CLAUDE.md).Review
1 round — no blocking findings (documentation checked against the code). Polish applied: no re-set of the interrupt flag when throwing,
@Throws, README on interruptedclose(), single interpreter factory default.Follow-ups
close()is interrupted (pre-existing, documented here).🤖 Generated with Claude Code
https://claude.ai/code/session_01FJmHQ3jHRbVdpwUiNUkApG