Add closed-session guard to CopilotSession#40
Conversation
Co-authored-by: brunoborges <129743+brunoborges@users.noreply.github.com>
Co-authored-by: brunoborges <129743+brunoborges@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR adds a fail-fast “closed session” guard to CopilotSession so that calling session APIs after close() throws an immediate IllegalStateException instead of triggering slow/opaque RPC failures.
Changes:
- Added an
isTerminatedlifecycle flag withensureNotTerminated()checks across session-interacting public APIs. - Made
CopilotSession.close()idempotent (safe to call multiple times / via try-with-resources). - Added a dedicated
ClosedSessionGuardTestsuite and updated an existing session test expectation.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| src/main/java/com/github/copilot/sdk/CopilotSession.java | Adds termination tracking, idempotent close(), and fail-fast guards on public session operations. |
| src/test/java/com/github/copilot/sdk/ClosedSessionGuardTest.java | New tests validating post-close behavior across guarded APIs and idempotent close semantics. |
| src/test/java/com/github/copilot/sdk/CopilotSessionTest.java | Updates expectations to align with new fail-fast IllegalStateException behavior. |
| IllegalStateException thrown = assertThrows(IllegalStateException.class, () -> { | ||
| session.send("test message"); | ||
| }); | ||
| assert thrown.getMessage().contains("closed"); | ||
| } |
There was a problem hiding this comment.
assert here is the Java assertion keyword, which is typically disabled in Maven test runs unless the JVM is started with -ea. That means this test may pass without actually verifying the exception message. Use JUnit assertions (e.g., assertTrue / assertEquals) instead (and apply the same fix to the other assert ... statements in this test class).
There was a problem hiding this comment.
Fixed in commit fac2e20. Replaced all Java assert statements with JUnit assertions (assertTrue with descriptive messages, assertEquals for value comparisons) to ensure proper test execution without requiring -ea flag.
|
|
||
| String pathAfterClose = session.getWorkspacePath(); | ||
| assert pathBeforeClose == pathAfterClose; // Both should be null or same value | ||
| } |
There was a problem hiding this comment.
This comparison uses == on Strings, which checks reference identity rather than value equality and can make the test flaky/incorrect. Use a JUnit value assertion (e.g., assertEquals) and explicitly handle the null case if getWorkspacePath() can be null.
There was a problem hiding this comment.
Fixed in commit fac2e20. Replaced == reference comparison with assertEquals() which properly handles both null and non-null String values using value equality.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: brunoborges <129743+brunoborges@users.noreply.github.com>
|
Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details. Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
Implementation Complete: Closed-Session Guard for CopilotSession
Summary
Successfully implemented fail-fast protection for
CopilotSessionthat prevents usage afterclose()has been called, providing clear error messages instead of cryptic RPC failures.Changes Made
Core Implementation (
CopilotSession.java)volatile boolean isTerminatedfield to track session lifecycle stateensureNotTerminated()validation method that throwsIllegalStateException("Session is closed")close()idempotent using synchronized block to safely handle multiple invocationssend(String)andsend(MessageOptions)sendAndWait(String),sendAndWait(MessageOptions),sendAndWait(MessageOptions, long)on(Consumer<AbstractSessionEvent>)andon(Class<T>, Consumer<T>)getMessages()abort()setEventErrorHandler(EventErrorHandler)setEventErrorPolicy(EventErrorPolicy)getSessionId()andgetWorkspacePath()still work after close (field reads for logging)Comprehensive Test Coverage (
ClosedSessionGuardTest.java)Created 15 new tests covering:
IllegalStateExceptionafter terminationclose()is idempotent (safe to call multiple times)assertkeyword with JUnit assertions (assertTrue,assertEquals) to ensure tests run properlyCompatibility Fix (
CopilotSessionTest.java)IllegalStateExceptionand legacy RPC errorsBenefits
java.iostreams,java.sql.Connection, etc.Test Results
✅ All 172 tests passing
✅ Code formatted with Spotless
✅ All test assertions use proper JUnit methods (no Java
assertkeyword)Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.