Skip to content

recv() returns stale proxy after slot is released to producer #20

@brianjenkins94

Description

@brianjenkins94

The SharedJsonBuffer returns proxy objects that point into shared memory. When recv() releases the slot (via slotsAvailable.release()), a producer can immediately overwrite that memory. If the consumer hasn't finished reading the value yet, it gets corrupted and ownKeys can return duplicates, and property reads return undefined.

One fix would be to copy the value out of shared memory before releasing the slot.

But I have mixed feelings about JSON.parse(JSON.stringify(val)).

--- a/src/lib/sync/mpmc.ts
+++ b/src/lib/sync/mpmc.ts
@@ recv()
+    const copied = JSON.parse(JSON.stringify(val));
     this.internals.slotsAvailable[INTERNAL_SEMAPHORE_CONTROLLER].release(1);
-    return { ok: true, value: val };
+    return { ok: true, value: copied };

@@ blockingRecv()
+    const copied = JSON.parse(JSON.stringify(val));
     this.internals.slotsAvailable[INTERNAL_SEMAPHORE_CONTROLLER].release(1);
-    return { ok: true, value: val };
+    return { ok: true, value: copied };

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions