Question about concurrency control in ResourceService (possible async mutex improvement)

Hi everyone,

I’m currently exploring the codebase as a prospective contributor and while reviewing packages/lib/services/ResourceService.ts, I noticed that indexNoteResources() appears to use a boolean flag (e.g., isIndexing_) to prevent concurrent execution.

From my understanding, the current pattern is roughly:

if (this.isIndexing_) return;
this.isIndexing_ = true;
// async work
this.isIndexing_ = false;

I was wondering whether this could potentially allow overlapping executions under certain asynchronous timing scenarios, especially if multiple calls are triggered very close together.

Would it make sense to replace this pattern with a lightweight async mutex abstraction to guarantee proper mutual exclusion? The idea would be:

  1. Ensure safe async mutual exclusion
  2. Avoid possible race conditions
  3. Provide a reusable utility for other services if needed

Before attempting any changes, I wanted to ask:

  1. Is this an intentional design decision?
  2. Has concurrency handling here been discussed before?
  3. Would maintainers be open to a small PR introducing an async mutex utility for this case?

I’m happy to investigate further or prototype a minimal implementation if this aligns with the project direction. Thanks!

Thanks for looking into this. Unless there's an actual bug we probably won't change the design though

1 Like

JavaScript/TypeScript async/await allows concurrency without multithreading. Tasks run to completion (or until an await or similar expression) before another task can start. See:

As such,

if (running) return;
running = true;

// other code

shouldn't allow multiple concurrent executions of "// other code", since there isn't an "await" between the if statement and the line that sets "running = true".

(Thank you for looking into this!)