diff --git a/lib/entrypoint_cli.dart b/lib/entrypoint_cli.dart index f7975f9..b11b844 100644 --- a/lib/entrypoint_cli.dart +++ b/lib/entrypoint_cli.dart @@ -140,7 +140,15 @@ For more details: https://github.com/jake-dev-99/quicktype_dart /// previous subscription before attaching a new one. static void _installLogForwarder() { Logger.root.level = Level.INFO; - _logSubscription?.cancel(); + // Fire-and-forget: cancel() stops delivering records synchronously; + // the Future it returns just signals completion of any async cleanup, + // which doesn't block attaching the next listener. + unawaited(_logSubscription?.cancel()); + // Subscription lives for the process lifetime (CLI is a short-lived + // binary); test re-entries cancel it via the unawaited() above. + // cancel_subscriptions fires on the .listen() site, not the field, + // so the ignore belongs here. + // ignore: cancel_subscriptions _logSubscription = Logger.root.onRecord.listen((record) { // Process stdout/stderr are owned by the runtime; never close them. if (record.level >= Level.WARNING) { @@ -151,8 +159,5 @@ For more details: https://github.com/jake-dev-99/quicktype_dart }); } - // ignore: cancel_subscriptions - // Subscription lives for the process lifetime; cancellation happens - // on re-entry (tests) via _installLogForwarder above. static StreamSubscription? _logSubscription; } diff --git a/native/shim/embed_bundle.py b/native/shim/embed_bundle.py index 974a0da..4c63478 100644 --- a/native/shim/embed_bundle.py +++ b/native/shim/embed_bundle.py @@ -14,6 +14,8 @@ const unsigned char qt_bundle_js[] — bundle bytes + trailing NUL const unsigned long qt_bundle_js_len — bundle length """ +import io +import os import sys # Characters that must not appear raw inside a C string literal. @@ -74,18 +76,35 @@ def main(): print(__doc__, file=sys.stderr) sys.exit(1) prelude, bundle, out_path = sys.argv[1:] + + # Buffer everything into memory first so a read failure on `prelude` + # or `bundle` (or a disk-full during emit) doesn't truncate the + # existing out_path. The atomic rename at the end guarantees + # consumers either see the complete old file or the complete new + # file — never a half-written one. + buf = io.BytesIO() + buf.write(b'// Auto-generated by embed_bundle.py \xe2\x80\x94 do not edit.\n\n') + emit(prelude, 'qt_prelude_js', buf) + emit(bundle, 'qt_bundle_js', buf) + + tmp_path = f'{out_path}.tmp-{os.getpid()}' try: - out = open(out_path, 'wb') + with open(tmp_path, 'wb') as tmp: + tmp.write(buf.getvalue()) + tmp.flush() + os.fsync(tmp.fileno()) + os.replace(tmp_path, out_path) except OSError as e: print( - f'embed_bundle.py: cannot open output {out_path} for writing: {e}', + f'embed_bundle.py: cannot write output {out_path}: {e}', file=sys.stderr, ) + if os.path.exists(tmp_path): + try: + os.unlink(tmp_path) + except OSError: + pass sys.exit(2) - with out: - out.write(b'// Auto-generated by embed_bundle.py \xe2\x80\x94 do not edit.\n\n') - emit(prelude, 'qt_prelude_js', out) - emit(bundle, 'qt_bundle_js', out) if __name__ == '__main__':