diff options
Diffstat (limited to 'packages/meshbay-cli/src/meshbay_cli/__main__.py')
| -rw-r--r-- | packages/meshbay-cli/src/meshbay_cli/__main__.py | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/packages/meshbay-cli/src/meshbay_cli/__main__.py b/packages/meshbay-cli/src/meshbay_cli/__main__.py index f5cee22..597e2fe 100644 --- a/packages/meshbay-cli/src/meshbay_cli/__main__.py +++ b/packages/meshbay-cli/src/meshbay_cli/__main__.py @@ -8,7 +8,9 @@ import argparse import asyncio import logging import os +import signal import sys +import threading from . import __version__ from .commands import ( @@ -113,15 +115,35 @@ async def _run(args: argparse.Namespace) -> int: await ctx.aclose() +class _WindowsSelectorLoop(asyncio.SelectorEventLoop): + """ + A selector loop that Ctrl+C wakes. Windows' select() is not interrupted by + a console event, and unlike the proactor (`windows_events.ProactorEventLoop. + run_forever`) this loop never points the signal wakeup at its self-pipe: Ctrl+C + waited for the next timer, which in `chat --follow` or `stream` can be half + a minute away (measured: 29 s on a loop whose next timer was 30 s off). + """ + + def run_forever(self): + if threading.current_thread() is not threading.main_thread(): + return super().run_forever() # signals reach the main thread only + old = signal.set_wakeup_fd(self._csock.fileno()) + try: + super().run_forever() + finally: + signal.set_wakeup_fd(old) + + def loop_factory(): """aiortc's ICE needs a selector loop: Windows' default proactor never completes a DataChannel handshake.""" - return asyncio.SelectorEventLoop if sys.platform == "win32" else None + return _WindowsSelectorLoop if sys.platform == "win32" else None def main(argv: list[str] | None = None) -> int: argv = sys.argv[1:] if argv is None else argv if argv[:1] == ["__complete"]: + _utf8_output() # what the completion scripts read from .complete import main as complete return complete(argv[1:]) if not argv and os.environ.get("MESHBAY_CLI_SELFTEST") == "1": |