Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure stream messages are always ordered #8059

Merged
merged 5 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions distributed/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1006,10 +1006,7 @@
break
handler = self.stream_handlers[op]
if iscoroutinefunction(handler):
self._ongoing_background_tasks.call_soon(
handler, **merge(extra, msg)
)
await asyncio.sleep(0)
await handler(**merge(extra, msg))
else:
handler(**merge(extra, msg))
else:
Expand Down Expand Up @@ -1521,6 +1518,14 @@

return _().__await__()

async def __aenter__(self):
await self
return self

Check warning on line 1523 in distributed/core.py

View check run for this annotation

Codecov / codecov/patch

distributed/core.py#L1522-L1523

Added lines #L1522 - L1523 were not covered by tests

async def __aexit__(self, *args):
await self.close()
return

Check warning on line 1527 in distributed/core.py

View check run for this annotation

Codecov / codecov/patch

distributed/core.py#L1526-L1527

Added lines #L1526 - L1527 were not covered by tests
Comment on lines +1521 to +1527
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an unrelated but pleasant addition to the CommPool


async def start(self) -> None:
# Invariant: semaphore._value == limit - open - _n_connecting
self.semaphore = asyncio.Semaphore(self.limit)
Expand Down
76 changes: 76 additions & 0 deletions distributed/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import dask

from distributed.batched import BatchedSend
from distributed.comm.core import CommClosedError
from distributed.comm.registry import backends
from distributed.comm.tcp import TCPBackend, TCPListener
Expand Down Expand Up @@ -1380,3 +1381,78 @@ async def test_async_listener_stop(monkeypatch):
async with Server({}) as s:
await s.listen(0)
assert s.listeners


@gen_test()
async def test_messages_are_ordered_bsend():
ledger = []

async def async_handler(val):
await asyncio.sleep(0.01)
hendrikmakait marked this conversation as resolved.
Show resolved Hide resolved
ledger.append(val)

def sync_handler(val):
ledger.append(val)

async with Server(
{},
stream_handlers={
"sync_handler": sync_handler,
"async_handler": async_handler,
},
) as s:
await s.listen()
comm = await connect(s.address)
try:
b = BatchedSend(interval=10)
try:
await comm.write({"op": "connection_stream"})
b.start(comm)
n = 100
for ix in range(n):
if ix % 2:
b.send({"op": "sync_handler", "val": ix})
else:
b.send({"op": "async_handler", "val": ix})
while not len(ledger) == n:
await asyncio.sleep(0.01)
assert ledger == list(range(n))
finally:
await b.close()
finally:
await comm.close()


@gen_test()
async def test_messages_are_ordered_raw():
ledger = []

async def async_handler(val):
await asyncio.sleep(0.01)
hendrikmakait marked this conversation as resolved.
Show resolved Hide resolved
ledger.append(val)

def sync_handler(val):
ledger.append(val)

async with Server(
{},
stream_handlers={
"sync_handler": sync_handler,
"async_handler": async_handler,
},
) as s:
await s.listen()
comm = await connect(s.address)
try:
await comm.write({"op": "connection_stream"})
n = 100
for ix in range(n):
if ix % 2:
await comm.write({"op": "sync_handler", "val": ix})
else:
await comm.write({"op": "async_handler", "val": ix})
while not len(ledger) == n:
await asyncio.sleep(0.01)
assert ledger == list(range(n))
finally:
await comm.close()
Loading