Skip to content

Commit

Permalink
Fix race condition when adding two requests with same name
Browse files Browse the repository at this point in the history
  • Loading branch information
psalagnac committed Jun 10, 2024
1 parent 7e87990 commit 09c3430
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,11 @@ public void submitAsyncTask(TaskObject taskObject) throws SolrException {

private void addTask(TaskObject taskObject) {
// Ensure task ID is not already in use
if (requestStatusCache.asMap().containsKey(taskObject.taskId)) {
TaskObject taskInCache = requestStatusCache.get(taskObject.taskId, n -> taskObject);

// If we get a different task instance, it means one was already in the cache with the
// same name. Just reject the new one.
if (taskInCache != taskObject) {
throw new SolrException(
ErrorCode.BAD_REQUEST, "Duplicate request with the same requestid found.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -569,4 +569,26 @@ public void testTrackedRequestExpiration() throws Exception {
asyncTracker.shutdown();
}
}

/** Check we reject a task is the async ID already exists. */
@Test
public void testDuplicatedRequestId() {

// Different tasks but with same ID
TaskObject task1 = new TaskObject("id1", "ACTION", false, null);
TaskObject task2 = new TaskObject("id1", "ACTION", false, null);

CoreAdminAsyncTracker asyncTracker = new CoreAdminAsyncTracker();
try {
asyncTracker.submitAsyncTask(task1);
try {
asyncTracker.submitAsyncTask(task2);
fail("Task should have been rejected.");
} catch (SolrException e) {
assertEquals("Duplicate request with the same requestid found.", e.getMessage());
}
} finally {
asyncTracker.shutdown();
}
}
}

0 comments on commit 09c3430

Please sign in to comment.