collective-soundworks / soundworks

Creative coding framework for distributed applications based on Web technologies.
http://soundworks.dev
BSD 3-Clause "New" or "Revised" License
114 stars 7 forks source link

[StateManager] Possible concurrency problem when creating SharedState #18

Closed b-ma closed 2 years ago

b-ma commented 3 years ago

Description

@note: observed w/ with multiple node clients running on the same computer as the server

cf. notes in @soundworks/plugin-checkin (server)

client-side

const state = await client.stateManager.create('some-state');

// this is sometime not called, somehow the notification  
// arrives before the `create` promise is resolved
state.subscribe(updates => {
  console.log(updates); 
});

server-side

stateManager.observe(async (schemaName, stateId, nodeId) => {
  const state = await stateManager.attach(schemaName, stateId, nodeId);
  // this does not always trigger the client-side subscribe
  state.set({ a: true });
});

Possible fix

cf. soundworks SharedState line 81 (UPDATE_NOTIFICATION), maybe this needs to be async (Promise.resolve().then(() => this._commit(updates))

b-ma commented 2 years ago

The fix introduces problems that are more important than the bug reported here (which I've never seen in a real application), so closing for now...