Skip to content

Cancel interrupted queued calls, document when close() returns early - #71

Merged
morisil merged 2 commits into
mainfrom
issue-29-close-salvage
Nov 13, 2026
Merged

morisil merged 2 commits into
mainfrom
issue-29-close-salvage

Conversation

@morisil

@morisil morisil commented Nov 11, 2026

Copy link
Copy Markdown
Member

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 first close(), 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:

  • InterruptedException is thrown with the standard contract (interrupt flag cleared) and declared with @Throws on exec, invoke, eval, get, set and close, so Java callers can catch it.
  • A failure of closing the interpreter is thrown by the close() waiting for it; later close() calls return quietly (try-with-resources / use report it once). Main's behavior, now documented and tested.
  • Tests pin that closing doesn't deadlock when __del__ reaches a Kotlin callback calling close(), directly or from another thread.
  • Internal test seam 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 interrupted close(), single interpreter factory default.

Follow-ups

🤖 Generated with Claude Code

https://claude.ai/code/session_01FJmHQ3jHRbVdpwUiNUkApG

morisil and others added 2 commits November 11, 2026 20:09
…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
morisil merged commit f7b4a2e into main Nov 13, 2026
@morisil
morisil deleted the issue-29-close-salvage branch November 13, 2026 15:59
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
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.

A second close() returns before the interpreter is closed, and an interrupted call still runs

1 participant