@@ -48,15 +48,51 @@ Object.defineProperty(globalThis, "__PDF_LOOPBACK_EGRESS_RECEIPT", {
4848} ) ;
4949
5050const { port1, port2 } = new MessageChannel ( ) ;
51+ const flushWaiters = new Map ( ) ;
52+ let nextFlushId = 0 ;
5153port1 . on ( "message" , message => {
5254 if ( message ?. type === "unreviewed_module_import_attempt" ) {
5355 telemetry . unreviewed_module_import_attempts += 1 ;
5456 if ( message . bare_package === true ) {
5557 telemetry . bare_package_import_attempts += 1 ;
5658 }
59+ return ;
60+ }
61+ if ( message ?. type === "telemetry_flush_ack" ) {
62+ const resolve = flushWaiters . get ( message . id ) ;
63+ flushWaiters . delete ( message . id ) ;
64+ resolve ?. ( ) ;
5765 }
5866} ) ;
5967port1 . unref ( ) ;
68+
69+ // Every other counter here is incremented synchronously, in this thread, by the
70+ // guard that denied the attempt. The two module-import counters are not: the
71+ // resolve hook runs on the loader thread and reports across this port, so the
72+ // import can have already rejected while its telemetry is still in flight.
73+ // Reading the receipt after a setImmediate assumed that a single turn of this
74+ // loop was enough for a cross-thread delivery. Usually it is. On a loaded
75+ // two-core runner it was not, and the calibration reported zero attempts for
76+ // exactly those two probes - a security calibration failing to prove its own
77+ // guards were live. Wait for a round trip instead of for a tick.
78+ Object . defineProperty ( globalThis , "__PDF_LOOPBACK_EGRESS_FLUSH" , {
79+ value ( ) {
80+ const id = nextFlushId ;
81+ nextFlushId += 1 ;
82+ return new Promise ( resolve => {
83+ flushWaiters . set ( id , resolve ) ;
84+ // The port is unreffed so it cannot hold the process open on its own;
85+ // hold it only for as long as this round trip is outstanding.
86+ port1 . ref ( ) ;
87+ port1 . postMessage ( { type : "telemetry_flush" , id } ) ;
88+ } ) . finally ( ( ) => {
89+ if ( flushWaiters . size === 0 ) port1 . unref ( ) ;
90+ } ) ;
91+ } ,
92+ configurable : false ,
93+ enumerable : false ,
94+ writable : false ,
95+ } ) ;
6096register ( "./no-package-loader.mjs" , import . meta. url , {
6197 data : { port : port2 } ,
6298 transferList : [ port2 ] ,
0 commit comments