kagkarlsson / db-scheduler

Persistent cluster-friendly scheduler for Java
Apache License 2.0
1.23k stars 188 forks source link

Potential race condition - JdbcTaskRepository#memoize #510

Open NicklasWallgren opened 2 months ago

NicklasWallgren commented 2 months ago

There is a risk that initialized is cached between threads, leading to multiple initializations.

Explaination;

  1. Thread 1 calls get and enters the firstTime method.
  2. Before firstTime completes, Thread 2 calls get.
  3. Since initialized might not be updated yet (due to thread interleaving), Thread 2 might also enter the firstTime method.
  4. Both threads could end up calling the original supplier and setting delegate and initialized, leading to multiple initializations.

Fix; Make initialized and delegate volatile to ensure visibility across threads.