classification
Title: [EASY][2.7] testInitNonExistentFile() of test_bz2 leaks references
Type: resource usage Stage: resolved
Components: Versions: Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: vstinner
Priority: normal Keywords: easy (C)

Created on 2017-06-09 15:26 by vstinner, last changed 2017-06-10 21:50 by vstinner. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 2031 closed matrixise, 2017-06-09 16:07
PR 2033 merged matrixise, 2017-06-09 16:51
Messages (4)
msg295542 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2017-06-09 15:26
haypo@selma$ ./python -m test -R 3:3 -m testInitNonExistentFile test_bz2
Run tests sequentially
0:00:00 [1/1] test_bz2
beginning 6 repetitions
123456
......
test_bz2 leaked [10, 10, 10] references, sum=30
1 test failed:
    test_bz2

Total duration: 81 ms
Tests result: FAILURE
[57793 refs]
msg295549 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2017-06-09 16:08
The leaking unit test was added to fix a bug found by fuzzing: see bpo-19878.

To fix the bug, I suggest to:

1) Add a clear method: extract most of BZ2File_dealloc() code except of code to clear the lock and the tp_free. Create a new BZ2File_clear() function which clears the file and buffers. Call BZ2File_clear() in BZ2File_dealloc(), but *before* destroying the lock (see later for the rationale). Replace Py_XDECREF(self->file) with Py_CLEAR(self->file). Add ACQUIRE_LOCK/RELEASE_LOCK in BZ2File_clear.

2) Maybe declare BZ2File_clear() as tp_clear. In this case, change BZ2File_clear() return type from void to int, and add "return 0" at the end

3) Move the lock initialization before "self->file = PyObject_CallFunction(...)" in BZ2File_init(). Modify the code to not create the lock twice: if the lock was already created by a previous call to BZ2File_init(), do nothing.

4) Call BZ2File_clear() in BZ2File_init() after the initialization of the lock
msg295634 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2017-06-10 12:36
New changeset 28288bebaf61269d1e26bb0795c2de6b481e1cf0 by Victor Stinner (St├ęphane Wirtel) in branch '2.7':
bpo-30614: testInitNonExistentFile() of test_bz2 leaks references (#2033)
https://github.com/python/cpython/commit/28288bebaf61269d1e26bb0795c2de6b481e1cf0
msg295674 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2017-06-10 21:50
The bug should now be fixed.
History
Date User Action Args
2017-06-10 21:50:34vstinnersetstatus: open -> closed
resolution: fixed
messages: + msg295674

stage: resolved
2017-06-10 12:37:00vstinnersetmessages: + msg295634
2017-06-09 16:51:27matrixisesetpull_requests: + pull_request2098
2017-06-09 16:08:38vstinnersetmessages: + msg295549
2017-06-09 16:07:19matrixisesetpull_requests: + pull_request2097
2017-06-09 15:26:03vstinnercreate