redis-om-node icon indicating copy to clipboard operation
redis-om-node copied to clipboard

repository.createIndex is not safe for concurrent use

Open golopot opened this issue 11 months ago • 3 comments

When repository.createIndex is called multiple times concurrently, it might throw an exception [ErrorReply: Index already exists]. This happens when test runner run app init code concurrently.

Another problem is that the thrown exception should be an Error.

Reproduction:

import { createClient } from "redis";
import { Schema } from "redis-om";
import { Repository } from "redis-om";

async function main() {
  const redis = createClient();
  await redis.connect();
  {
    const schema = new Schema("s", {
      a: { type: "string" },
    });
    const repo = new Repository(schema, redis);
    await repo.createIndex();
  }
  {
    const schema = new Schema("s", {
      a: { type: "string" },
      b: { type: "string" },
    });
    const repo = new Repository(schema, redis);
    await Promise.all([
      repo.createIndex(),
      repo.createIndex(),
      repo.createIndex(),
      repo.createIndex(),
      repo.createIndex(),
    ]);
  }

  return redis.quit();
}

main().catch((err) => {
  console.error("err at main", err);
});

golopot avatar Nov 06 '24 09:11 golopot