Skip to content

Commit 527f53c

Browse files
authored
Fix: 'close(force: true)' does not cause uncaught exception (#404)
1 parent 38bc774 commit 527f53c

File tree

3 files changed

+8
-16
lines changed

3 files changed

+8
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## 3.4.6
44

55
- Fix: do not close connection after an empty statement.
6+
- Fix: `close(force: true)` does not cause uncaught exception.
67

78
## 3.4.5
89

lib/src/v3/connection.dart

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,8 @@ class PgConnectionImplementation extends _PgSessionBase implements Connection {
583583

584584
@override
585585
Future<void> close({bool force = false}) async {
586-
await _close(force, null);
586+
final ex = force ? PgException('Connection closed.') : null;
587+
await _close(force, ex);
587588
}
588589

589590
Future<void> _close(bool interruptRunning, PgException? cause,
@@ -594,9 +595,6 @@ class PgConnectionImplementation extends _PgSessionBase implements Connection {
594595
try {
595596
if (interruptRunning) {
596597
_pending?.handleConnectionClosed(cause);
597-
if (!_socketIsBroken) {
598-
_channel.sink.add(const TerminateMessage());
599-
}
600598
} else {
601599
// Wait for the previous operation to complete by using the lock
602600
await _operationLock.withResource(() {
@@ -823,8 +821,6 @@ class _PgResultStreamSubscription
823821
Future<ResultSchema> get schema => _schema.future;
824822

825823
Future<void> _completeQuery() async {
826-
_done.complete();
827-
828824
// Make sure the affectedRows and schema futures complete with something
829825
// after the query is done, even if we didn't get a row description
830826
// message.
@@ -834,6 +830,7 @@ class _PgResultStreamSubscription
834830
if (!_schema.isCompleted) {
835831
_schema.complete(ResultSchema(const []));
836832
}
833+
_done.complete();
837834
await _controller.close();
838835
}
839836

test/v3_close_test.dart

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,7 @@ void main() {
108108
await Future.delayed(const Duration(milliseconds: 100));
109109
await expectConn1ClosesForcefully(conn);
110110

111-
// TODO: this should be throwing PgException
112-
await expectLater(() => rs, isNotNull);
111+
await expectLater(() => rs, throwsA(isA<PgException>()));
113112
});
114113
});
115114

@@ -120,17 +119,13 @@ void main() {
120119
final rs = conn.runTx((tx) async {
121120
started.complete();
122121
await runLongQuery(tx);
123-
})
124-
// Ignore async error, it will fail when the connection is closed and it tries to do COMMIT
125-
// TODO: remove this ignore
126-
.ignore();
122+
});
127123
// let it start
128124
await started.future;
129125
await Future.delayed(const Duration(milliseconds: 100));
130126
await expectConn1ClosesForcefully(conn);
131127

132-
// TODO: this should be throwing PgException
133-
await expectLater(() => rs, isNotNull);
128+
await expectLater(() => rs, throwsA(isA<PgException>()));
134129
});
135130
});
136131

@@ -147,8 +142,7 @@ void main() {
147142
await Future.delayed(const Duration(milliseconds: 100));
148143
await expectConn1ClosesForcefully(conn);
149144

150-
// TODO: this should be throwing PgException
151-
await expectLater(() => rs, isNotNull);
145+
await expectLater(() => rs, throwsA(isA<PgException>()));
152146
});
153147
});
154148
});

0 commit comments

Comments
 (0)