classification
Title: Segfault when closing BufferedWriter from a different thread
Type: crash Stage: resolved
Components: Interpreter Core Versions: Python 3.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: benfogle, benjamin.peterson, martin.panter, pitrou, stutzbach
Priority: normal Keywords: patch

Created on 2017-11-08 04:27 by benfogle, last changed 2017-11-10 21:05 by pitrou. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 4331 merged benfogle, 2017-11-08 04:35
Messages (3)
msg305803 - (view) Author: Benjamin Fogle (benfogle) * Date: 2017-11-08 04:27
To reproduce:
```
import threading
import io
import time
import _pyio

class MyFileIO(io.FileIO):
    def flush(self):
        # Simulate a slow flush. Slow disk, etc.
        time.sleep(0.25)
        super().flush()

raw = MyFileIO('test.dat', 'wb')
#fp = _pyio.BufferedWriter(raw)
fp = io.BufferedWriter(raw)
t1 = threading.Thread(target=fp.close)
t1.start()
time.sleep(0.1) # Ensure t1 is sleeping in fp.close()/raw.flush()
fp.write(b'test')
t1.join()
```

_pyio.BufferedWriter ignores the error completely rather than throwing a ValueError("write to closed file").
msg306048 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2017-11-10 21:03
New changeset 9703f092abc0259926d88c7855afeae4a78afc7d by Antoine Pitrou (benfogle) in branch 'master':
bpo-31976: Fix race condition when flushing a file is slow. (#4331)
https://github.com/python/cpython/commit/9703f092abc0259926d88c7855afeae4a78afc7d
msg306049 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2017-11-10 21:04
Thank you for posting this Benjamin.  As I said on the PR, I don't think I want to backport this to 3.6, as it is always delicate to reason about subclassing and threads.
History
Date User Action Args
2017-11-10 21:05:15pitrousetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2017-11-10 21:04:47pitrousetmessages: + msg306049
versions: - Python 3.6
2017-11-10 21:03:43pitrousetnosy: + pitrou
messages: + msg306048
2017-11-08 21:32:58pitrousetnosy: + martin.panter
2017-11-08 21:32:38pitrousetnosy: + benjamin.peterson, stutzbach
2017-11-08 04:53:08benfoglesettype: crash
2017-11-08 04:35:45benfoglesetkeywords: + patch
stage: patch review
pull_requests: + pull_request4287
2017-11-08 04:27:27benfoglecreate