Skip to content

[Bug]: WorkspaceIndex leaks its SQLite connection when initialization fails #3911

Closed

Description

@pengmoubuaixuexi

Describe the bug

WorkspaceIndex.open() acquires a SQLite connection, then runs a PRAGMA and initializes its schema. If initialization fails, the catch block returns null without closing that connection. No WorkspaceIndex is returned, so callers cannot release the acquired resource.

This is reproducible with a real corrupt .index/workspace.db, not just an injected SQL exception. The optional-index fallback still works, but the acquired native/database resource is left open instead of being deterministically released. Repeated workspace initialization can repeat this resource leak.

To Reproduce

On main@4399f75fcdc699465733b4a4dc71794c741b92c9 (2.1.0-SNAPSHOT), create a temporary workspace and write non-SQLite bytes to .index/workspace.db. The following regression uses a real SQLite connection; the JDBC static interception only retains a reference to the connection used by the public factory:

Path dbFile = Files.createDirectories(root.resolve(".index")).resolve("workspace.db");
Files.writeString(dbFile, "not a SQLite database".repeat(10));
String url = "jdbc:sqlite:" + dbFile.toAbsolutePath();
try (var connection = JDBC.createConnection(url, new Properties());
        var connections = mockStatic(JDBC.class, CALLS_REAL_METHODS)) {
    connections.when(() -> JDBC.createConnection(eq(url), any(Properties.class)))
            .thenReturn(connection);
    assertNull(WorkspaceIndex.open(root));
    assertTrue(connection.isClosed(), "failed initialization must release its connection");
}

Command from the repository root:

mvn -B -T1C -pl :agentscope-harness -am -Dtest=WorkspaceIndexLifecycleTest,WorkspaceIndexPrefixMatchTest -Dsurefire.failIfNoSpecifiedTests=false test

Actual result: 7 tests, 1 failure, 0 errors. The corrupt-database lifecycle case fails; the successful-open ownership case and all 5 existing prefix cases pass. The test itself closes the captured connection in finally through try-with-resources.

Expected behavior

Initialization failure returns null and closes any connection already acquired. Successful initialization transfers ownership to the returned index; that connection stays open until WorkspaceIndex.close().

Error messages

WorkspaceIndexLifecycleTest.corruptDatabaseClosesConnectionWhenInitializationFails
failed initialization must release its connection ==> expected: <true> but was: <false>

Environment

  • AgentScope Java: 2.1.0-SNAPSHOT, main 4399f75fcdc699465733b4a4dc71794c741b92c9
  • Java: Eclipse Temurin 17.0.15
  • OS: Windows
  • SQLite JDBC: 3.47.1.0
  • Maven: 3.9.12

Additional context

Root cause: WorkspaceIndex.java factory acquires the connection before the PRAGMA/schema setup, but cleanup only exists on successfully returned instances. The initialization catch block does not own/close that connection. Relevant source:

public static WorkspaceIndex open(Path workspaceRoot) {
try {
Path indexDir = workspaceRoot.resolve(INDEX_DIR);
Files.createDirectories(indexDir);
Path dbFile = indexDir.resolve(INDEX_DB);
String url = "jdbc:sqlite:" + dbFile.toAbsolutePath();
Connection c = DriverManager.getConnection(url);
try (Statement st = c.createStatement()) {
st.execute("PRAGMA case_sensitive_like = ON");
}
WorkspaceIndex idx = new WorkspaceIndex(c);
idx.initSchema();
return idx;
} catch (Exception e) {
log.warn("WorkspaceIndex unavailable (non-fatal): {}", e.getMessage());
return null;
}
}
private WorkspaceIndex(Connection conn) {
this.conn = conn;
}
// -------------------------------------------------------------------------
// Schema
// -------------------------------------------------------------------------

Searched open/closed issues and PRs for WorkspaceIndex, initSchema, WorkspaceIndex.open, workspace.db, connection leaks and initialization. Existing prefix/fallback work (#3875, #3197/#3201) and builder concurrency #3719 have different root causes.

I am implementing the small cleanup fix and will submit the linked regression-tested PR in this session. Please assign this issue to @pengmoubuaixuexi if appropriate; I am taking responsibility for the implementation rather than requesting someone else to pick it up.

AI assistance: Codex assisted with source investigation, the regression test and the proposed fix. The reproduction above was executed locally; no model/provider credentials or external services were used.

Activity

  1. added a commit that references this issue on Nov 3, 2026
    2b4a1cb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions