Skip to content

Commit 0555859

Browse files
committed
Fix poll.poll test so it doesn't leak resources
1 parent 3520372 commit 0555859

1 file changed

Lines changed: 23 additions & 5 deletions

File tree

‎Lib/test/test_poll.py‎

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -204,18 +204,36 @@ def test_threaded_poll(self):
204204
os.write(w, b'spam')
205205
t.join()
206206

207+
@unittest.skipUnless(threading, 'Threading required for this test.')
208+
@reap_threads
207209
def test_poll_blocks_with_negative_ms(self):
208210

209-
def poll_exec():
210-
pollster = select.poll()
211-
pollster.poll(-1e-100)
211+
# GIVEN
212+
213+
# Create two file descriptors. This will be used to unlock
214+
# the blocking call to poll.poll inside the thread
215+
216+
r, w = os.pipe()
217+
self.addCleanup(os.close, r)
218+
self.addCleanup(os.close, w)
212219

213-
poll_thread = threading.Thread(target=poll_exec)
214-
poll_thread.daemon = True
220+
pollster = select.poll()
221+
pollster.register(r, select.POLLIN)
222+
223+
# WHEN
224+
225+
poll_thread = threading.Thread(target=pollster.poll, args=(-1e-100,))
215226
poll_thread.start()
216227
poll_thread.join(timeout=0.1)
228+
229+
# THEN
217230
self.assertTrue(poll_thread.is_alive())
218231

232+
# Write to the pipe so pollster.poll unblocks and the thread ends.
233+
os.write(w, b'spam')
234+
poll_thread.join()
235+
self.assertFalse(poll_thread.is_alive())
236+
219237

220238

221239
def test_main():

0 commit comments

Comments
 (0)