@@ -91,24 +91,22 @@ async def test_stdin_eof_monitor_detects_hangup():
9191 mock_buffer = MagicMock ()
9292 mock_buffer .fileno .return_value = read_fd
9393
94- cancelled = False
9594 with patch .object (sys , "platform" , "linux" ), patch .object (sys , "stdin" , MagicMock (buffer = mock_buffer )):
96- with anyio .CancelScope () as scope :
97- async with anyio .create_task_group () as tg :
98- monitor = _create_stdin_eof_monitor (tg )
99- assert monitor is not None
100- tg .start_soon (monitor )
101-
102- # Close the write end to trigger POLLHUP on read end
103- os .close (write_fd )
104- write_fd = - 1
105-
106- # The monitor will cancel the task group scope when it
107- # detects POLLHUP. Wait with a timeout to avoid hanging.
108- with anyio .fail_after (5 ):
109- await anyio .sleep (10 ) # will be cancelled by monitor
110- cancelled = scope .cancel_called
111- assert cancelled
95+ async with anyio .create_task_group () as tg :
96+ monitor = _create_stdin_eof_monitor (tg )
97+ assert monitor is not None
98+ tg .start_soon (monitor )
99+
100+ # Close the write end to trigger POLLHUP on read end
101+ os .close (write_fd )
102+ write_fd = - 1
103+
104+ # Wait for the monitor to cancel the task-group scope.
105+ with anyio .fail_after (5 ):
106+ while not tg .cancel_scope .cancel_called :
107+ await anyio .sleep (0.05 )
108+
109+ assert tg .cancel_scope .cancel_called
112110 finally :
113111 os .close (read_fd )
114112 if write_fd != - 1 : # pragma: no cover
@@ -143,9 +141,12 @@ async def test_stdin_eof_monitor_ignores_pollin_events():
143141 os .close (write_fd )
144142 write_fd = - 1
145143
146- # Wait for the monitor to detect POLLHUP and cancel
144+ # Wait for the monitor to detect POLLHUP and cancel.
147145 with anyio .fail_after (5 ):
148- await anyio .sleep (10 ) # will be cancelled by monitor
146+ while not tg .cancel_scope .cancel_called :
147+ await anyio .sleep (0.05 )
148+
149+ assert tg .cancel_scope .cancel_called
149150 finally :
150151 os .close (read_fd )
151152 if write_fd != - 1 : # pragma: no cover
0 commit comments