Skip to content

Log a failure of closing the interpreter which no close() waits for - #86

Open
morisil wants to merge 5 commits into
mainfrom
issue-69-close-failure-interrupted
Open

morisil wants to merge 5 commits into
mainfrom
issue-69-close-failure-interrupted

Conversation

@morisil

@morisil morisil commented Nov 11, 2026

Copy link
Copy Markdown
Member

Fixes #69

Stacked on #71 (fix of #29) — merge #71 into main first (bottom-up, see CLAUDE.md); GitHub retargets this PR when the base branch is deleted.

A failure of closing the interpreter is no longer lost when no close() waits for it — the waiting close() was interrupted, or close() returned at once because a call was running: it is logged once, as a WARNING of the System.Logger com.xemantic.kotlin.script.python.Python (no new dependency).

  • The closing task and the close() that stops waiting race on one state (ClosingState: Abandoned / Failed); whichever comes second logs, so the failure is reported exactly once — thrown by the waiting close(), or logged.
  • Cancel interrupted queued calls, document when close() returns early #71's contract holds: later close() calls return at once, the close() waiting for the closing throws its failure, use / try-with-resources unchanged.
  • Logging never throws (a failing backend can't swallow the failure or the InterruptedException), and the logger is created lazily.
  • KDoc and README say where the failure goes.

Review

1 round — no blocking findings (exactly-once and the contract traced through every path). Polish applied: runCatching around logging, lazy logger, typed state, test log capture filtered by exception instance and without parent handlers, KDoc order.

🤖 Generated with Claude Code

https://claude.ai/code/session_01FJmHQ3jHRbVdpwUiNUkApG

morisil and others added 4 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
…#69)

When the waiting close() was interrupted, or returned at once with a call
running, a failure of closing the interpreter stayed in a future nobody
read. It is now logged as a WARNING of the System.Logger of Python,
exactly once, by whichever side comes second: the closing task, or the
close() giving up on it. A close() waiting for it still throws it, and
nothing is logged then.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FJmHQ3jHRbVdpwUiNUkApG
#69)

The logging of a failure of closing the interpreter cannot throw anymore,
so the closing task still rethrows the failure, and an interrupted close()
still throws its InterruptedException. The logger is created only when
needed, the closing state is a sealed type, and a failure thrown by close()
is no longer kept. The log capture of the tests filters by the expected
failure, and does not pass the records on to the console.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FJmHQ3jHRbVdpwUiNUkApG
Base automatically changed from issue-29-close-salvage to main November 13, 2026 15:59
#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.

1 participant