Skip to content

Problems with buffer cleanup during interpreter shutdown #561

Description

@jholveck

I'm opening this issue to track it myself; you can just assign it to me if you like.

In some code I was testing, I had inadvertently caused some tests to fail. Ok, that's fine; that's how development goes. But at the end of PyTest, I got this error:

.../venv/lib/python3.13/site-packages/_pytest/unraisableexception.py:67: PytestUnraisableExceptionWarning: Exception ignored in __release_buffer__ of _FinalizingBufferIntermediate: None

Traceback (most recent call last):
  File "/home/joelh/src/python-mss/src/mss/buffer.py", line 219, in __release_buffer__
    self._finalizer()
    ~~~~~~~~~~~~~~~^^
  File "/home/joelh/src/python-mss/src/mss/linux/xshmgetimage.py", line 224, in _release_shm_slot
    with self._shm_lock:
         ^^^^^^^^^^^^^^
AttributeError: 'MSSImplXShmGetImage' object has no attribute '_shm_lock'


  warnings.warn(pytest.PytestUnraisableExceptionWarning(msg))

Here, it seems that during interpreter shutdown, some objects are being destroyed in an unexpected order. Interpreter shutdown can break cycles by removing individual references, even if they're still in use. Here, it seems that the _shm_lock attribute of the MSSImplXShmGetImage object was removed before a memoryview that uses it was finalized.

I'd like to make sure that we correctly clean up the buffer even in shutdown; for instance, we don't want an embedded Python using MSS to leak. I also want to make sure users don't get a confusing error message.

I haven't yet verified my hypothesis, so I'm avoiding simple hacks to suppress or work around it. I'm working on making a repro case, and then will properly diagnose and fix this.

So far, the best repro I've got is to have my terminal running full-screen, and run pytest -k test_implementation. Some of the tests will take two screenshots and expect them to be identical (a separate concern). If the screen is changing, then those can differ, causing the test to fail. The above finalization error only happens when I get such a failure, but not every time I get such a failure.

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions