weak-promise.https.any.js (1952B)
1 // META: global=window,worker 2 // META: script=/common/get-host-info.sub.js 3 // META: script=/webtransport/resources/webtransport-test-helpers.sub.js 4 // META: script=/common/utils.js 5 // META: script=/common/gc.js 6 7 'use strict'; 8 9 async function timeout(cmd) { 10 const timer = new Promise((resolve, reject) => { 11 const id = setTimeout(() => { 12 clearTimeout(id) 13 reject(new Error("Promise timed out!")) 14 }, 750) 15 }) 16 return Promise.race([cmd, timer]) 17 } 18 19 // This is a test for bug 1958291 20 // A weakref should deref to null if the object has been GCd 21 // but a bug in the implementation of NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN 22 // made it so that only its members were GCd, leading to target.ready dereferencing 23 // a null pointer. 24 promise_test(async t => { 25 // Create first WebTransport connection and a WeakRef to it 26 let wt1 = new WebTransport( 27 webtransport_url(`query.py?token=${token()}`) 28 ); 29 const weakref = new WeakRef(wt1); 30 31 // Create second WebTransport connection 32 const wt2 = new WebTransport( 33 webtransport_url(`query.py?token=${token()}`) 34 ); 35 t.add_cleanup(() => wt2.close()); 36 37 // Remove strong reference to wt1 38 wt1 = undefined; 39 40 await wt2.ready; 41 42 // Trigger garbage collection 43 await garbageCollect(); 44 45 // Create bidirectional streams on wt2 46 for (let i = 0; i < 8; i++) { 47 await timeout(wt2.createBidirectionalStream({})); 48 } 49 50 // Try to dereference the weakref 51 const target = weakref.deref(); 52 53 // The object may have been garbage collected (target === undefined) 54 // or it may still be alive (target !== undefined). Both are valid outcomes. 55 if (target !== undefined) { 56 // Object survived GC, verify it's still accessible 57 try { 58 await timeout(target.ready); 59 } catch (e) { 60 // Timeout or connection error is acceptable 61 } 62 } 63 // If target is undefined, the object was successfully garbage collected 64 }, 'WebTransport garbage collection behavior with WeakRef');