Skip to content

[Bug]: ref errors with shared instances #203

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mirageisland-ai opened this issue May 17, 2025 · 1 comment
Closed

[Bug]: ref errors with shared instances #203

mirageisland-ai opened this issue May 17, 2025 · 1 comment

Comments

@mirageisland-ai
Copy link

mirageisland-ai commented May 17, 2025

Bug description

We ran into a few errors related to refs when moving instances through various different schemas. I've provided a reproduction below.

For context, we basically have a sandbox-like game world which replicates its schema state 1:1 from the server to clients. The game world's state can be arbitrarily complex, as it stores not only arbitrary game state, but also visual properties of all entities in the game (such as mesh shapes, sizes, textures, etc.). So the game is largely "server-side rendered". Representing all this state in an ergonomic way often requires sharing and nesting schema instances across different schema objects.

I think we might be pushing the limits of the current schema system, or possibly even using it in an unintended way 😅

Optional: Minimal reproduction

Since the issues seem to be of the form "errors when sharing instances across schemas", I've created a PR here with a few new test cases showcasing the errors: #202

If it's helpful, I could also split this into different Github issues

Running the full test suite should result in this output:

  332 passing (965ms)
  9 pending
  3 failing

  1) Encoder
       dynamic schema operations
         should handle moving shared instances between array <-> map <-> field:
     Error: "refId" not found: 4
      at Decoder.decode (src/decoder/Decoder.ts:67:39)
      at _.src_1.Schema.decode (test/Schema.ts:81:29)
      at encodeDecodeAndAssert (test/Encoder.test.ts:40:26)
      at Context.<anonymous> (test/Encoder.test.ts:75:13)
      at processImmediate (node:internal/timers:483:21)

  2) Encoder
       dynamic schema operations
         should replace instances multiple times in nested structures:

      AssertionError [ERR_ASSERTION]: refCount mismatch for refId=1

1 !== 0

      + expected - actual

      -1
      +0
      
      at encodeDecodeAndAssert (test/Encoder.test.ts:44:24)
      at Context.<anonymous> (test/Encoder.test.ts:108:13)
      at processImmediate (node:internal/timers:483:21)

  3) Encoder
       dynamic schema operations
         should survive clear & repopulate on ArraySchema with shared children:
     Error: "refId" not found: 1
      at Decoder.decode (src/decoder/Decoder.ts:67:39)
      at _.src_1.Schema.decode (test/Schema.ts:81:29)
      at encodeDecodeAndAssert (test/Encoder.test.ts:40:26)
      at Context.<anonymous> (test/Encoder.test.ts:139:13)
      at processImmediate (node:internal/timers:483:21)
@mirageisland-ai
Copy link
Author

Closing this issue as the original tests I submitted were flawed. There may still be issues to look into, but they seem related to incorrect ref counting when decoding rather than producing "refId" not found errors.

See the discussion here for more details: #202 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant