Skip to content

Commit 533a649

Browse files
committed
bugfix: Write HAR on Ctrl+C for mobile preference --out
The `--out` file was never written. `SnifferPreference.sniff()` guarded the write with `except KeyboardInterrupt`, but under the asyncio `async_command` wrapper Ctrl+C raises `KeyboardInterrupt` at the `run_until_complete` level (outside the coroutine) and then cancels the task, so the coroutine actually receives `asyncio.CancelledError` at the `async for`. That never matched the handler, so the HAR was silently dropped. Move the write into a `finally` and catch `asyncio.CancelledError` alongside `KeyboardInterrupt`, and flush to ensure the file hits disk on exit. Claude-Session: https://claude.ai/code/session_012YD9YmyR2AQ7Gmpc1LGSTz
1 parent fec6196 commit 533a649

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

harlogger/sniffers.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import asyncio
12
import json
23
import os
34
import posixpath
@@ -136,11 +137,17 @@ def __init__(
136137
}
137138

138139
async def sniff(self) -> None:
140+
# Under the asyncio ``async_command`` wrapper, Ctrl+C surfaces inside the coroutine as a
141+
# ``CancelledError`` (the wrapper cancels the task) rather than a ``KeyboardInterrupt``, so the
142+
# HAR must be flushed from a ``finally`` to actually be written on exit.
139143
try:
140144
await self._sniff()
141-
except KeyboardInterrupt:
145+
except (KeyboardInterrupt, asyncio.CancelledError):
146+
pass
147+
finally:
142148
if self.out:
143149
self.out.write(json.dumps(self.har, indent=4))
150+
self.out.flush()
144151

145152
async def _sniff(self) -> None:
146153
incomplete = ""

0 commit comments

Comments
 (0)