Skip to content

Commit

Permalink
fix: No longer fatal errors on inactive thread pool (#44)
Browse files Browse the repository at this point in the history
Previously, trying to open a connection on an inactive thread pool would throw `Fatal error: leaking promise created at (file: "SQLiteNIO/SQLiteConnection.swift", line: 101)`
  • Loading branch information
NeedleInAJayStack authored Mar 17, 2023
1 parent 596b680 commit 766d6cc
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
8 changes: 2 additions & 6 deletions Sources/SQLiteNIO/SQLiteConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,6 @@ public final class SQLiteConnection: SQLiteDatabase {
path = file
}

let promise = eventLoop.makePromise(of: SQLiteConnection.self)

return threadPool.runIfActive(eventLoop: eventLoop) {
var handle: OpaquePointer?
let options = SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE | SQLITE_OPEN_FULLMUTEX | SQLITE_OPEN_URI
Expand All @@ -112,13 +110,11 @@ public final class SQLiteConnection: SQLiteDatabase {
on: eventLoop
)
logger.debug("Connected to sqlite db: \(path)")
return promise.succeed(connection)
return connection
} else {
logger.error("Failed to connect to sqlite db: \(path)")
return promise.fail(SQLiteError(reason: .cantOpen, message: "Cannot open SQLite database: \(storage)"))
throw SQLiteError(reason: .cantOpen, message: "Cannot open SQLite database: \(storage)")
}
}.flatMap {
promise.futureResult
}
}

Expand Down
7 changes: 7 additions & 0 deletions Tests/SQLiteNIOTests/SQLiteNIOTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ final class SQLiteNIOTests: XCTestCase {
let compileOptions = try conn.query("PRAGMA compile_options;").wait()
print(compileOptions)
}

func testConnectionClosedThreadPool() throws {
let threadPool = NIOThreadPool(numberOfThreads: 1)
try threadPool.syncShutdownGracefully()
// This should error, but not create a leaking promise fatal error
XCTAssertThrowsError(try SQLiteConnection.open(storage: .memory, threadPool: threadPool, on: self.eventLoop).wait())
}

func testZeroLengthBlob() throws {
let conn = try SQLiteConnection.open(storage: .memory, threadPool: self.threadPool, on: self.eventLoop).wait()
Expand Down

0 comments on commit 766d6cc

Please sign in to comment.