One request in my NFL data pipeline could finish twice. Each request fetches one page of data for the player and team pages on Statpro, my stats site for sports fans. A fetching library runs the job and calls a success handler to save the result or a failure handler to record the error. It could fire both for the same request, whenever my handler outlasted the library's own timeout.
That gave one request two possible endings, and only one can be right. The extra ending either persisted the result a second time, which risks duplicate data, or threw an error into a void.
First claim wins, late arrivals join
The fix was part of seven pull requests on that NFL pipeline in one day. Each request ID now gets exactly one handler whose outcome counts. Whichever handler fires first claims it, and anything that arrives later joins the existing promise:
claim(requestId, callback) {
const owned = this.owned.get(requestId);
if (owned) return { ownership: "joiner", result: owned };
const claimed = Promise.resolve().then(callback);
this.owned.set(requestId, claimed);
return { ownership: "owner", result: claimed };
}
The library only runs a limited number of handlers at once. A late arrival that just joins gives up its place in that limit immediately, so nothing sits blocked behind it. Before the job exits, I wait for the first handler's work to finish, so no write is left half-finished.
The smaller fixes around it
Player profile pages that lack the machine-readable data the job reads no longer crash the pass, which is one full run of this fetching job. The timeout for the step that collects the raw data went from 120 to 300 seconds, and the handler timeout is now bounded separately. A few players on team rosters couldn't be matched to a player record Statpro already has. I now accept those few as gaps in the data.
Own the coordination when you own the side effects
The library owned the timeout and I owned the writes. The race lived in that split, because the library couldn't know my handler was still working. When a library decides that your code is done and your code has side effects, add your own layer that decides which outcome counts.