Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions core/src/main/java/com/google/adk/flows/llmflows/Contents.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,19 @@ public Single<RequestProcessor.RequestProcessingResult> processRequest(
modelName = "";
}

ImmutableList<Event> sessionEvents;
synchronized (context.session().events()) {
sessionEvents = ImmutableList.copyOf(context.session().events());
}

if (llmAgent.includeContents() == LlmAgent.IncludeContents.NONE) {
return Single.just(
RequestProcessor.RequestProcessingResult.create(
request.toBuilder()
.contents(
getCurrentTurnContents(
context.branch().orElse(null),
context.session().events(),
sessionEvents,
context.agent().name(),
modelName))
.build(),
Expand All @@ -80,10 +85,7 @@ public Single<RequestProcessor.RequestProcessingResult> processRequest(

ImmutableList<Content> contents =
getContents(
context.branch().orElse(null),
context.session().events(),
context.agent().name(),
modelName);
context.branch().orElse(null), sessionEvents, context.agent().name(), modelName);

return Single.just(
RequestProcessor.RequestProcessingResult.create(
Expand Down
60 changes: 60 additions & 0 deletions core/src/test/java/com/google/adk/flows/llmflows/ContentsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,13 @@
import com.google.genai.types.FunctionCall;
import com.google.genai.types.FunctionResponse;
import com.google.genai.types.Part;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicReference;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
Expand Down Expand Up @@ -780,6 +783,63 @@ public void processRequest_notEmptyContent() {
assertThat(contents).containsExactly(e.content().get());
}

@Test
public void processRequest_concurrentReadAndWrite_noException() throws Exception {
LlmAgent agent =
LlmAgent.builder().name(AGENT).includeContents(LlmAgent.IncludeContents.DEFAULT).build();
Session session =
sessionService
.createSession("test-app", "test-user", new HashMap<>(), "test-session")
.blockingGet();

// Seed with dummy events to widen the race capability
for (int i = 0; i < 5000; i++) {
session.events().add(createUserEvent("dummy" + i, "dummy"));
}

InvocationContext context =
InvocationContext.builder()
.invocationId("test-invocation")
.agent(agent)
.session(session)
.sessionService(sessionService)
.build();

LlmRequest initialRequest = LlmRequest.builder().build();

AtomicReference<Throwable> writerError = new AtomicReference<>();
CountDownLatch startLatch = new CountDownLatch(1);

Thread writerThread =
new Thread(
() -> {
startLatch.countDown();
try {
for (int i = 0; i < 2000; i++) {
session.events().add(createUserEvent("writer" + i, "new data"));
}
} catch (Throwable t) {
writerError.set(t);
}
});

writerThread.start();
startLatch.await(); // wait for writer to be ready

// Process (read) requests concurrently to trigger race conditions
for (int i = 0; i < 200; i++) {
var unused = contentsProcessor.processRequest(context, initialRequest).blockingGet();
if (writerError.get() != null) {
throw new RuntimeException("Writer failed", writerError.get());
}
}

writerThread.join();
if (writerError.get() != null) {
throw new RuntimeException("Writer failed", writerError.get());
}
}

private static Event createUserEvent(String id, String text) {
return Event.builder()
.id(id)
Expand Down
Loading