Skip to content

Conversation

@jholveck
Copy link
Contributor

In the XShmGetImage backend, there was a window of time during which a KeyboardInterrupt (or other asynchronous exception) would cause cleanup of the MSS object to raise a different exception. This is one of the slower parts of this backend, so it's a time when asynchronous exceptions can hit pretty easily.

While a Python library almost never can guarantee correctness following an asynchronous exception, ending a program with Ctrl-C is not uncommon. We can't guarantee correctness in all circumstances, and shouldn't try to.

All this patch does is to avoid the exception that may be hard for an end user to understand. Instead, the top-level user code will now see the KeyboardInterrupt, and not the seemingly-unrelated cleanup exception.

Changes proposed in this PR

  • Tests added/updated
  • Documentation updated - N/A, no user-visible changes
  • Changelog entry added - if the existing entry for Implement an XShmGetImage-based backend #431 is sufficient; this is a bug fix for that code
  • ./check.sh passed

In the XShmGetImage backend, there was a window of time during which a
KeyboardInterrupt (or other asynchronous exception) would cause
cleanup of the MSS object to raise a different exception.  This is one
of the slower parts of this backend, so it's a time when asynchronous
exceptions can hit pretty easily.

While a Python library almost never can guarantee correctness
following an asynchronous exception, ending a program with Ctrl-C is
not uncommon.  We can't guarantee correctness in all circumstances,
and shouldn't try to.

All this patch does is to avoid the exception that may be hard for an
end user to understand.  Instead, the top-level user code will now see
the KeyboardInterrupt, and not the seemingly-unrelated cleanup
exception.
@BoboTiG BoboTiG merged commit 0822b33 into BoboTiG:main Jan 28, 2026
21 checks passed
BoboTiG pushed a commit that referenced this pull request Jan 28, 2026
* Bug fix: KeyboardInterrupt while copying data from an mmapped region

In the XShmGetImage backend, there was a window of time during which a
KeyboardInterrupt (or other asynchronous exception) would cause
cleanup of the MSS object to raise a different exception.  This is one
of the slower parts of this backend, so it's a time when asynchronous
exceptions can hit pretty easily.

While a Python library almost never can guarantee correctness
following an asynchronous exception, ending a program with Ctrl-C is
not uncommon.  We can't guarantee correctness in all circumstances,
and shouldn't try to.

All this patch does is to avoid the exception that may be hard for an
end user to understand.  Instead, the top-level user code will now see
the KeyboardInterrupt, and not the seemingly-unrelated cleanup
exception.

* Switch to a simpler implementation

I just learned that memoryviews can be used as context managers, and
release their buffers at the end of that, instead of having to be
GC'd.
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.

2 participants