Skip to content

Commit 98a2369

Browse files
committed
Fix #10 with workaround for underlying BlueSSLService issue
1 parent 54ef859 commit 98a2369

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

Sources/PostgresClientKit/Connection.swift

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,12 +1120,22 @@ public class Connection: CustomStringConvertible {
11201120
readBufferPosition = 0
11211121

11221122
var readCount = 0
1123+
let timeout = Date(timeIntervalSinceNow: 30.0) // 30 seconds
11231124

1124-
do {
1125-
readCount = try socket.read(into: &readBuffer)
1126-
} catch {
1127-
log(.warning, "Error receiving response: \(error)")
1128-
throw PostgresError.socketError(cause: error)
1125+
while readCount == 0 && Date() < timeout && !socket.remoteConnectionClosed {
1126+
do {
1127+
readCount = try socket.read(into: &readBuffer)
1128+
1129+
if readCount == 0 {
1130+
// Workaround https://github.com/IBM-Swift/BlueSSLService/issues/79.
1131+
// This issue results in socket.read(...) returning 0 even though the
1132+
// socket is supposedly "blocking".
1133+
Thread.sleep(forTimeInterval: 0.01) // 10 ms
1134+
}
1135+
} catch {
1136+
log(.warning, "Error receiving response: \(error)")
1137+
throw PostgresError.socketError(cause: error)
1138+
}
11291139
}
11301140

11311141
if readCount == 0 {

0 commit comments

Comments
 (0)