@@ -202,6 +202,8 @@ class PgConnectionImplementation extends _PgSessionBase implements Connection {
202
202
static Future <PgConnectionImplementation > connect (
203
203
Endpoint endpoint, {
204
204
ConnectionSettings ? connectionSettings,
205
+ @visibleForTesting
206
+ StreamTransformer <Uint8List , Uint8List >? incomingBytesTransformer,
205
207
}) async {
206
208
final settings = connectionSettings is ResolvedConnectionSettings
207
209
? connectionSettings
@@ -217,6 +219,7 @@ class PgConnectionImplementation extends _PgSessionBase implements Connection {
217
219
endpoint,
218
220
settings,
219
221
codecContext: codecContext,
222
+ incomingBytesTransformer: incomingBytesTransformer,
220
223
);
221
224
222
225
if (_debugLog) {
@@ -257,6 +260,7 @@ class PgConnectionImplementation extends _PgSessionBase implements Connection {
257
260
Endpoint endpoint,
258
261
ResolvedConnectionSettings settings, {
259
262
required CodecContext codecContext,
263
+ StreamTransformer <Uint8List , Uint8List >? incomingBytesTransformer,
260
264
}) async {
261
265
final host = endpoint.host;
262
266
final port = endpoint.port;
@@ -337,6 +341,10 @@ class PgConnectionImplementation extends _PgSessionBase implements Connection {
337
341
adaptedStream = async .SubscriptionStream (subscription);
338
342
}
339
343
344
+ if (incomingBytesTransformer != null ) {
345
+ adaptedStream = adaptedStream.transform (incomingBytesTransformer);
346
+ }
347
+
340
348
final outgoingSocket = async .StreamSinkExtensions (socket)
341
349
.transform <Uint8List >(
342
350
async .StreamSinkTransformer .fromHandlers (handleDone: (out) {
@@ -373,6 +381,7 @@ class PgConnectionImplementation extends _PgSessionBase implements Connection {
373
381
final bool _channelIsSecure;
374
382
late final StreamSubscription <Message > _serverMessages;
375
383
bool _isClosing = false ;
384
+ bool _socketIsBroken = false ;
376
385
377
386
_PendingOperation ? _pending;
378
387
// Errors happening while a transaction is active will roll back the
@@ -558,19 +567,21 @@ class PgConnectionImplementation extends _PgSessionBase implements Connection {
558
567
559
568
Future <void > _close (bool interruptRunning, PgException ? cause,
560
569
{bool socketIsBroken = false }) async {
570
+ _socketIsBroken = _socketIsBroken || socketIsBroken;
561
571
if (! _isClosing) {
562
572
_isClosing = true ;
563
573
564
574
if (interruptRunning) {
565
575
_pending? .handleConnectionClosed (cause);
566
- if (! socketIsBroken ) {
576
+ if (! _socketIsBroken ) {
567
577
_channel.sink.add (const TerminateMessage ());
568
578
}
569
579
} else {
570
580
// Wait for the previous operation to complete by using the lock
571
581
await _operationLock.withResource (() {
572
- // Use lock to await earlier operations
573
- _channel.sink.add (const TerminateMessage ());
582
+ if (! _socketIsBroken) {
583
+ _channel.sink.add (const TerminateMessage ());
584
+ }
574
585
});
575
586
}
576
587
@@ -580,7 +591,11 @@ class PgConnectionImplementation extends _PgSessionBase implements Connection {
580
591
}
581
592
582
593
void _closeAfterError ([PgException ? cause]) {
583
- _close (true , cause);
594
+ _close (
595
+ true ,
596
+ cause,
597
+ socketIsBroken: cause? .willAbortConnection ?? false ,
598
+ );
584
599
}
585
600
}
586
601
0 commit comments