|
| 1 | +// Copyright (c) 2026 Cloudflare, Inc. |
| 2 | +// Licensed under the Apache 2.0 license found in the LICENSE file or at: |
| 3 | +// https://opensource.org/licenses/Apache-2.0 |
| 4 | + |
| 5 | +#include "jsg-test.h" |
| 6 | + |
| 7 | +namespace workerd::jsg::test { |
| 8 | +namespace { |
| 9 | + |
| 10 | +// This test covers a shutdown failure involving a wrapped parent that owns an unwrapped child. |
| 11 | +// RefHolder is returned to JavaScript and therefore has a JS wrapper and a cppgc shim. Child is |
| 12 | +// never exposed to JavaScript, so it has no wrapper of its own, but tracing RefHolder propagates |
| 13 | +// the isolate pointer to it through RefHolder::visitForGc(). |
| 14 | +// |
| 15 | +// A major GC can condemn RefHolder's wrapper while leaving its cppgc finalizer pending. Isolate |
| 16 | +// teardown must finalize RefHolder while the isolate lock is held so that releasing its Ref<Child> |
| 17 | +// can destroy Child immediately. If RefHolder instead remains owned by its condemned shim until |
| 18 | +// v8::Isolate::Dispose(), its finalizer runs after the deferred-destruction queue has transitioned |
| 19 | +// to DROPPED. Child still remembers the isolate and sees that the isolate lock is not held, so it |
| 20 | +// tries to enqueue its destruction and fails the queueState == ACTIVE requirement. |
| 21 | +// |
| 22 | +// Production stacks reached the same path through container objects such as Response and R2 |
| 23 | +// GetResult releasing ReadableStream references from CppgcShim::~CppgcShim(). |
| 24 | +V8System v8System({"--expose-gc"_kj}); |
| 25 | + |
| 26 | +class ContextGlobalObject: public Object, public ContextGlobal {}; |
| 27 | + |
| 28 | +uint childDestructions = 0; |
| 29 | +uint holderDestructions = 0; |
| 30 | + |
| 31 | +class Child final: public Object { |
| 32 | + public: |
| 33 | + ~Child() noexcept(false) { |
| 34 | + ++childDestructions; |
| 35 | + } |
| 36 | + |
| 37 | + JSG_RESOURCE_TYPE(Child) {} |
| 38 | +}; |
| 39 | + |
| 40 | +class RefHolder final: public Object { |
| 41 | + public: |
| 42 | + explicit RefHolder(Ref<Child> child): child(kj::mv(child)) {} |
| 43 | + |
| 44 | + ~RefHolder() noexcept(false) { |
| 45 | + ++holderDestructions; |
| 46 | + } |
| 47 | + |
| 48 | + void visitForGc(GcVisitor& visitor) { |
| 49 | + visitor.visit(child); |
| 50 | + } |
| 51 | + |
| 52 | + JSG_RESOURCE_TYPE(RefHolder) {} |
| 53 | + |
| 54 | + private: |
| 55 | + Ref<Child> child; |
| 56 | +}; |
| 57 | + |
| 58 | +struct ShutdownContext: public ContextGlobalObject { |
| 59 | + Ref<RefHolder> makeHolder(Lock& js) { |
| 60 | + return js.alloc<RefHolder>(js.alloc<Child>()); |
| 61 | + } |
| 62 | + |
| 63 | + JSG_RESOURCE_TYPE(ShutdownContext) { |
| 64 | + JSG_NESTED_TYPE(Child); |
| 65 | + JSG_NESTED_TYPE(RefHolder); |
| 66 | + JSG_METHOD(makeHolder); |
| 67 | + } |
| 68 | +}; |
| 69 | +JSG_DECLARE_ISOLATE_TYPE(ShutdownIsolate, ShutdownContext, Child, RefHolder); |
| 70 | + |
| 71 | +KJ_TEST("isolate shutdown finalizes condemned wrappers containing unwrapped children") { |
| 72 | + setPredictableModeForTest(); |
| 73 | + childDestructions = 0; |
| 74 | + holderDestructions = 0; |
| 75 | + |
| 76 | + { |
| 77 | + ShutdownIsolate isolate(v8System, kj::heap<IsolateObserver>()); |
| 78 | + isolate.runInLockScope([&](ShutdownIsolate::Lock& lock) { |
| 79 | + JSG_WITHIN_CONTEXT_SCOPE( |
| 80 | + lock, lock.newContext<ShutdownContext>().getHandle(lock), [&](Lock& js) { |
| 81 | + js.withinHandleScope([&] { |
| 82 | + // Drop the only JS reference while the nested handle scope ensures no temporary V8 |
| 83 | + // handle keeps RefHolder's wrapper alive. |
| 84 | + auto source = "let holder = makeHolder(); holder = null;"_kj; |
| 85 | + auto script = check(v8::Script::Compile(js.v8Context(), js.str(source))); |
| 86 | + check(script->Run(js.v8Context())); |
| 87 | + }); |
| 88 | + |
| 89 | + // Forced test GCs normally sweep atomically. Leave sweeping pending to reproduce the state |
| 90 | + // created by a natural major GC immediately before isolate shutdown. |
| 91 | + js.requestGcWithDeferredSweepForTesting(); |
| 92 | + // Prove that neither cppgc finalization nor child destruction happened during the GC. This |
| 93 | + // keeps the test focused on shutdown rather than the ordinary atomic-sweep path. |
| 94 | + KJ_ASSERT(holderDestructions == 0, holderDestructions); |
| 95 | + KJ_ASSERT(childDestructions == 0, childDestructions); |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + // The isolate is destroyed with RefHolder's cppgc finalizer still pending. Isolate teardown |
| 100 | + // must finalize the holder and release its unwrapped child safely. |
| 101 | + } |
| 102 | + |
| 103 | + KJ_ASSERT(holderDestructions == 1, holderDestructions); |
| 104 | + KJ_ASSERT(childDestructions == 1, childDestructions); |
| 105 | +} |
| 106 | + |
| 107 | +} // namespace |
| 108 | +} // namespace workerd::jsg::test |
0 commit comments