Skip to content

Commit 95c3c53

Browse files
fix(runtime): treat a claim whose adapter is being released as released
While an adapter's teardown waits on its isolate's loop, the claim stays on the JS object and loads as nil. interop.bufferFromData asserted on it, and interop.handleof released a null reference. GetValueOrReport detaches such a claim and reports the object as released, as marshalling it would, and handleof returns no handle for it.
1 parent 487062e commit 95c3c53

5 files changed

Lines changed: 62 additions & 3 deletions

File tree

‎NativeScript/runtime/Helpers.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,8 @@ enum class ReleasedObjectPolicy {
336336
ReleasedObjectPolicy GetReleasedObjectPolicy();
337337
void SetReleasedObjectPolicy(ReleasedObjectPolicy policy);
338338

339-
// Like GetValue, but applies ReleasedObjectPolicy when the object carries no
340-
// wrapper: throws (kThrow) or schedules a `releasednativeaccess` event
339+
// Like GetValueForMarshalling, but applies ReleasedObjectPolicy when the object
340+
// carries no wrapper: throws (kThrow) or schedules a `releasednativeaccess` event
341341
// (kReport, deduplicated per object), returning nullptr either way.
342342
// `operation` names the API surface for the event payload / error message.
343343
// Callers must no-op on nullptr; paths that would otherwise read the internal

‎NativeScript/runtime/Helpers.mm‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ bool ApplyReleasedObjectPolicy(Isolate* isolate, const Local<Value>& val, const
625625

626626
tns::BaseDataWrapper* tns::GetValueOrReport(Isolate* isolate, const Local<Value>& val,
627627
const char* operation) {
628-
BaseDataWrapper* wrapper = tns::GetValue(isolate, val);
628+
BaseDataWrapper* wrapper = tns::GetValueForMarshalling(isolate, val);
629629
if (wrapper != nullptr) {
630630
return wrapper;
631631
}

‎NativeScript/runtime/InteropTypes.mm‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,10 @@
738738
ObjCDataWrapper* w = static_cast<ObjCDataWrapper*>(wrapper);
739739
@autoreleasepool {
740740
id target = w->Data();
741+
// A claim whose adapter's last release has started is no handle.
742+
if (target == nil) {
743+
break;
744+
}
741745
CFTypeRef ref = CFBridgingRetain(target);
742746
void* handle = const_cast<void*>(ref);
743747
CFRelease(ref);

‎TestRunner/app/tests/GCFinalizerTests.js‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,44 @@ describe("GC finalizer callbacks", function () {
812812
};
813813
});
814814

815+
// While this isolate runs JS, teardowns the worker hands off wait, and
816+
// the claims stay on their objects with their adapters released.
817+
it("treats a claim whose adapter another isolate released as released", function (done) {
818+
worker = new Worker("./adapterClaimReleaseWorker.js");
819+
var object = { value: 1 };
820+
var bytes = new Uint8Array([1, 2]);
821+
worker.onmessage = function (msg) {
822+
if (msg.data === "released") {
823+
done();
824+
return;
825+
}
826+
var center = NSNotificationCenter.defaultCenter;
827+
center.postNotificationNameObjectUserInfo("adapter-claim-release", bytes, null);
828+
center.postNotificationNameObjectUserInfo("adapter-claim-release", object, null);
829+
// The notifications stay autoreleased until this turn ends.
830+
timer = setTimeout(function () {
831+
worker.postMessage("release");
832+
// The worker releases the bytes' adapter first, so it is
833+
// released once the object's handle is gone.
834+
var handleReleased = false;
835+
var deadline = Date.now() + 5000;
836+
while (!handleReleased && Date.now() < deadline) {
837+
try {
838+
interop.handleof(object);
839+
} catch (e) {
840+
handleReleased = true;
841+
}
842+
}
843+
expect(handleReleased).toBe(true);
844+
expect(interop.bufferFromData(bytes)).toBeUndefined();
845+
});
846+
};
847+
worker.onerror = function (e) {
848+
expect(String(e && e.message ? e.message : e)).toBe("<no worker error>");
849+
done();
850+
};
851+
});
852+
815853
// The main thread posts under its Locker, then waits for the worker's
816854
// observer. The worker collects notifications under its own Locker,
817855
// dropping native objects whose teardown needs the main isolate.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Keeps the parent's notifications until asked, then releases them on this
2+
// worker's thread in the order they arrived.
3+
var name = "adapter-claim-release";
4+
var notifications = [];
5+
var observer = NSNotificationCenter.defaultCenter.addObserverForNameObjectQueueUsingBlock(
6+
name, null, null, function (notification) { notifications.push(notification); });
7+
8+
onmessage = function () {
9+
NSNotificationCenter.defaultCenter.removeObserver(observer);
10+
notifications.forEach(function (notification) {
11+
__releaseNativeCounterpart(notification);
12+
});
13+
notifications = [];
14+
postMessage("released");
15+
};
16+
17+
postMessage("observing");

0 commit comments

Comments
 (0)