Security Operations

A team made a shared account-transfer function thread-safe by locking both the source and destination account records. Under load the service intermittently hangs, and thread dumps show two threads each holding one lock and waiting for the other. Which change fixes this?

Show the answer

Correct answerD. Acquire the locks in the same fixed order in every code path

AWhy not this: More threads just add more blocked workers; the two threads holding the locks still wait on each other.

BWhy not this: Memory safety prevents memory-corruption flaws; it does not change the order in which locks are taken.

CWhy not this: An atomic counter cannot guard a two-record invariant, so the transfer would lose its protection.

DWhy this is right: A single global lock order prevents the circular wait in which each thread holds what the other needs.

Two threads locking the same pair of records in opposite orders produce a deadlock. A consistent global lock order breaks the circular wait and keeps the function thread-safe. A larger thread pool only adds more threads that can become stuck.