8000 inproc: Session.close() swallows cancellation · Issue #38 · nixidae/nixidae · GitHub
[go: up one dir, main page]

Skip to content

inproc: Session.close() swallows cancellation #38

Closed

Description

@Lillecarl

inproc.Session.close() collects BaseException into an error list, so a
CancelledError never reaches the scope that owns it.

Evidence

nanopynix/src/nanopynix/inproc/_impl.py:324-329:

async def close_resource(operation: Any) -> None:
    try:
        await operation
    except BaseException as exc:
        errors.append(exc)

CancelledError is a BaseException. It lands in errors and leaves as a
plain raise or as a BaseExceptionGroup (:331-334). The executor.shutdown
handler at :345 does the same thing.

The rpc engine catches Exception only, and its comment
(rpc/client/session.py:256-262) gives the reason: a converted cancellation
never reaches the scope that owns it, so an enclosing fail_after cannot turn
its own expiry into a TimeoutError. rpc/client/_pool.py:287-302 records
that this same defect was present there and was removed.

Caution

inproc/_impl.py has three other except BaseException handlers, at :267,
:981 and :1227. Those three are correct. Do not change them. Each runs
a cleanup and then re-raises, which is the one shape that must catch a
cancellation. The comment at :981 explains why its cleanup cannot be
skipped: an abandoned thread stays registered with the Boehm GC.

Scope

Catch Exception in close_resource (:328) and in the executor.shutdown
handler (:345).

Risk

Low. The rpc engine has run this way since its own correction.

Acceptance criteria

CancelledError propagates out of a cancelled close(), and
anyio.fail_after around close() raises TimeoutError.

Tests

Two tests, beside the existing rpc test
test_a_cancelled_close_still_stops_the_worker_process.

Compatibility

This is a deliberate behaviour change for inproc. A caller that catches the
BaseExceptionGroup loses its match. The change aligns inproc with rpc.

Finding F4 of CODE_IMPROVEMENT_PLAN.md.

Metadata

Metadata

Assignees

No one assigned

    Labels

    source: nanopynix

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0