Skip to content

Commit b84ef60

Browse files
committed
Follow-up to the "weak references to tasks" heisenbug
Store strong references into a global set as well. Hopefully can get removed one day, as python/cpython#121264 was merged just this week :)
1 parent e9bb771 commit b84ef60

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

ircstream/main.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131

3232
logger = structlog.get_logger()
3333

34+
# holder for strong references to pending tasks; remove when the minimum CPython version is one with PR#121264
35+
background_tasks = set()
36+
3437

3538
def parse_args(argv: Sequence[str] | None) -> argparse.Namespace:
3639
"""Parse and return the parsed command line arguments."""
@@ -92,13 +95,17 @@ async def start_servers(config: configparser.ConfigParser) -> None:
9295
ircserver = IRCServer(config["irc"])
9396
irc_coro = ircserver.serve()
9497
irc_task = asyncio.create_task(irc_coro)
98+
background_tasks.add(irc_task)
99+
irc_task.add_done_callback(background_tasks.discard)
95100
else:
96101
logger.critical('Invalid configuration, missing section "irc"')
97102
raise SystemExit(-1)
98103

99104
if "rc2udp" in config:
100105
rc2udp_coro = RC2UDPServer(config["rc2udp"], ircserver).serve()
101-
rc2udp_task = asyncio.create_task(rc2udp_coro) # noqa: F841 pylint: disable=unused-variable
106+
rc2udp_task = asyncio.create_task(rc2udp_coro)
107+
background_tasks.add(rc2udp_task)
108+
rc2udp_task.add_done_callback(background_tasks.discard)
102109
else:
103110
logger.warning("RC2UDP is not enabled in the config; server usefulness may be limited")
104111

0 commit comments

Comments
 (0)