Skip to content
Merged
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
14 changes: 11 additions & 3 deletions src/main/java/com/github/copilot/sdk/CopilotSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -542,9 +542,17 @@ public CompletableFuture<AssistantMessageEvent> sendAndWait(MessageOptions optio
}
}

// When inner future completes, run cleanup and propagate to result
// When inner future completes, run cleanup and propagate to result.
// Use whenCompleteAsync so that result.complete(r) is not called
// synchronously on the event-dispatch thread while dispatchEvent() is
// still iterating over handlers. Without async dispatch, a caller that
// registered its own session.on() listener before calling sendAndWait()
// could see its listener invoked *after* result.get() returned, because
// sendAndWait's internal handler would complete the future mid-loop. By
// submitting the completion to timeoutScheduler we allow the current
// dispatch loop to finish calling all other handlers first.
final ScheduledFuture<?> taskToCancel = timeoutTask;
future.whenComplete((r, ex) -> {
future.whenCompleteAsync((r, ex) -> {
try {
subscription.close();
} catch (IOException e) {
Expand All @@ -560,7 +568,7 @@ public CompletableFuture<AssistantMessageEvent> sendAndWait(MessageOptions optio
result.complete(r);
}
}
});
}, timeoutScheduler);

// When result is cancelled externally, cancel inner future to trigger cleanup
result.whenComplete((v, ex) -> {
Expand Down
18 changes: 16 additions & 2 deletions src/test/java/com/github/copilot/sdk/TestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,27 @@
import java.nio.file.Paths;

/**
* Shared test utilities for locating the Copilot CLI binary.
* Shared test utilities for locating the Copilot CLI binary and other
* cross-platform test helpers.
*/
final class TestUtil {
public final class TestUtil {

private TestUtil() {
}

/**
* Returns a platform-independent path string for a file inside the system
* temporary directory. Uses {@code java.io.tmpdir} so tests run correctly on
* both POSIX and Windows.
*
* @param filename
* the file name (no directory separator required)
* @return absolute path string in the system temp directory
*/
public static String tempPath(String filename) {
return Path.of(System.getProperty("java.io.tmpdir"), filename).toString();
}

/**
* Locates a launchable Copilot CLI executable.
* <p>
Expand Down
Loading
Loading