Skip to content

Commit 6f3e02f

Browse files
committed
Fix serial task implementation in SQLClient actor
1 parent c89c6fb commit 6f3e02f

1 file changed

Lines changed: 14 additions & 10 deletions

File tree

Sources/SQLClientSwift/SQLClient.swift

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,8 @@ public actor SQLClient {
134134
private let queue = DispatchQueue(label: "com.sqlclient.serial")
135135
private var activeTask: Task<Void, Never>?
136136

137-
private func serialize<T: Sendable>(_ operation: @escaping @Sendable () async throws -> T) async throws -> T {
138-
let newTask: Task<T, Error> = Task { [activeTask] in
139-
_ = await activeTask?.result
140-
return try await operation()
141-
}
142-
activeTask = Task { _ = await newTask.result }
143-
return try await newTask.value
137+
private func awaitPrevious() async {
138+
_ = await activeTask?.result
144139
}
145140

146141
public var maxTextSize: Int = 4096
@@ -153,7 +148,8 @@ public actor SQLClient {
153148
}
154149

155150
public func connect(options: SQLClientConnectionOptions) async throws {
156-
try await serialize {
151+
await awaitPrevious()
152+
let task = Task {
157153
guard !self.connected else { throw SQLClientError.alreadyConnected }
158154

159155
let result = try await self.runBlocking {
@@ -164,10 +160,13 @@ public actor SQLClient {
164160
self.connection = result.connection.pointer
165161
self.connected = true
166162
}
163+
activeTask = Task { _ = await task.result }
164+
try await task.value
167165
}
168166

169167
public func disconnect() async {
170-
_ = try? await serialize {
168+
await awaitPrevious()
169+
let task = Task {
171170
guard self.connected else { return }
172171
let lgn = self.login.map { TDSHandle(pointer: $0) }
173172
let conn = self.connection.map { TDSHandle(pointer: $0) }
@@ -178,10 +177,13 @@ public actor SQLClient {
178177
self.connection = nil
179178
self.connected = false
180179
}
180+
activeTask = Task { _ = await task.result }
181+
_ = await task.result
181182
}
182183

183184
public func execute(_ sql: String) async throws -> SQLClientResult {
184-
try await serialize {
185+
await awaitPrevious()
186+
let task = Task {
185187
guard self.connected, let conn = self.connection else { throw SQLClientError.notConnected }
186188
guard !sql.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else { throw SQLClientError.noCommandText }
187189
let maxText = self.maxTextSize
@@ -191,6 +193,8 @@ public actor SQLClient {
191193
return try self._executeSync(sql: sql, connection: handle, maxTextSize: maxText)
192194
}
193195
}
196+
activeTask = Task { _ = await task.result }
197+
return try await task.value
194198
}
195199

196200
public func query(_ sql: String) async throws -> [SQLRow] { try await execute(sql).rows }

0 commit comments

Comments
 (0)