Skip to content

Commit 63e8a7b

Browse files
bhuvi27miss-islington
authored andcommitted
gh-145030: Fix asyncio write pipe transport for named FIFOs on macOS (GH-154222)
(cherry picked from commit 409f324) Co-authored-by: Bhuvi <b.chouksey27@gmail.com>
1 parent 3871923 commit 63e8a7b

3 files changed

Lines changed: 60 additions & 2 deletions

File tree

Lib/asyncio/unix_events.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,8 @@ def __init__(self, loop, pipe, protocol, waiter=None, extra=None):
650650
self._conn_lost = 0
651651
self._closing = False # Set when close() or write_eof() called.
652652

653-
mode = os.fstat(self._fileno).st_mode
653+
pipe_stat = os.fstat(self._fileno)
654+
mode = pipe_stat.st_mode
654655
is_char = stat.S_ISCHR(mode)
655656
is_fifo = stat.S_ISFIFO(mode)
656657
is_socket = stat.S_ISSOCK(mode)
@@ -667,7 +668,19 @@ def __init__(self, loop, pipe, protocol, waiter=None, extra=None):
667668
# On AIX, the reader trick (to be notified when the read end of the
668669
# socket is closed) only works for sockets. On other platforms it
669670
# works for pipes and sockets. (Exception: OS X 10.4? Issue #19294.)
670-
if is_socket or (is_fifo and not sys.platform.startswith("aix")):
671+
# On macOS, the trick misfires for named FIFOs (but not for pipes
672+
# created with os.pipe(), which have st_nlink == 0): the write end
673+
# polls as readable whenever unread data sits in the FIFO, and no
674+
# event is delivered when the read end is closed, so it can only
675+
# ever report a false disconnection (gh-145030). The same xnu
676+
# behaviour applies on iOS/tvOS/watchOS (sys.platform is not
677+
# "darwin" there).
678+
is_named_fifo_on_apple = (
679+
sys.platform in {"darwin", "ios", "tvos", "watchos"}
680+
and is_fifo and pipe_stat.st_nlink > 0)
681+
if is_socket or (is_fifo
682+
and not sys.platform.startswith("aix")
683+
and not is_named_fifo_on_apple):
671684
# only start reading when connection_made() has been called
672685
self._loop.call_soon(self._loop._add_reader,
673686
self._fileno, self._read_ready)

Lib/test/test_asyncio/test_events.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1609,6 +1609,48 @@ def reader(data):
16091609
self.loop.run_until_complete(proto.done)
16101610
self.assertEqual('CLOSED', proto.state)
16111611

1612+
@unittest.skipUnless(sys.platform != 'win32',
1613+
"Don't support pipes for Windows")
1614+
@unittest.skipUnless(hasattr(os, 'mkfifo'), 'requires os.mkfifo()')
1615+
def test_write_named_fifo_unread_data(self):
1616+
# gh-145030: on macOS, the write end of a named FIFO polls as
1617+
# readable while unread data sits in the FIFO, which made the
1618+
# transport misinterpret the event as the reader hanging up
1619+
# and close itself.
1620+
path = os_helper.TESTFN
1621+
os.mkfifo(path)
1622+
self.assertNotEqual(os.stat(path).st_nlink, 0)
1623+
self.addCleanup(os_helper.unlink, path)
1624+
rfd = os.open(path, os.O_RDONLY | os.O_NONBLOCK)
1625+
self.addCleanup(os.close, rfd)
1626+
wfd = os.open(path, os.O_WRONLY | os.O_NONBLOCK)
1627+
pipeobj = io.open(wfd, 'wb', 1024)
1628+
1629+
proto = MyWritePipeProto(loop=self.loop)
1630+
connect = self.loop.connect_write_pipe(lambda: proto, pipeobj)
1631+
transport, p = self.loop.run_until_complete(connect)
1632+
self.assertIs(p, proto)
1633+
self.assertEqual('CONNECTED', proto.state)
1634+
1635+
transport.write(b'1')
1636+
# Iterate the event loop while the data stays unread in the FIFO;
1637+
# the transport must not detect a false disconnection.
1638+
for _ in range(10):
1639+
test_utils.run_briefly(self.loop)
1640+
self.assertEqual('CONNECTED', proto.state)
1641+
self.assertFalse(transport.is_closing())
1642+
self.assertEqual(b'1', os.read(rfd, 1024))
1643+
1644+
transport.write(b'2345')
1645+
for _ in range(10):
1646+
test_utils.run_briefly(self.loop)
1647+
self.assertEqual('CONNECTED', proto.state)
1648+
self.assertEqual(b'2345', os.read(rfd, 1024))
1649+
1650+
transport.close()
1651+
self.loop.run_until_complete(proto.done)
1652+
self.assertEqual('CLOSED', proto.state)
1653+
16121654
@unittest.skipUnless(sys.platform != 'win32',
16131655
"Don't support pipes for Windows")
16141656
def test_write_pipe_disconnect_on_close(self):
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix :mod:`asyncio` write pipe transports for named FIFOs on macOS. Unread
2+
data sitting in the FIFO made the transport misinterpret a poll event as
3+
the reader disconnecting, wrongly closing the transport.

0 commit comments

Comments
 (0)