Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: 'close(force: true)' does not cause uncaught exception #404

Merged
merged 1 commit into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 3.4.6

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

## 3.4.5

Expand Down
9 changes: 3 additions & 6 deletions lib/src/v3/connection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,8 @@ class PgConnectionImplementation extends _PgSessionBase implements Connection {

@override
Future<void> close({bool force = false}) async {
await _close(force, null);
final ex = force ? PgException('Connection closed.') : null;
await _close(force, ex);
}

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

Future<void> _completeQuery() async {
_done.complete();

// Make sure the affectedRows and schema futures complete with something
// after the query is done, even if we didn't get a row description
// message.
Expand All @@ -834,6 +830,7 @@ class _PgResultStreamSubscription
if (!_schema.isCompleted) {
_schema.complete(ResultSchema(const []));
}
_done.complete();
await _controller.close();
}

Expand Down
14 changes: 4 additions & 10 deletions test/v3_close_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ void main() {
await Future.delayed(const Duration(milliseconds: 100));
await expectConn1ClosesForcefully(conn);

// TODO: this should be throwing PgException
await expectLater(() => rs, isNotNull);
await expectLater(() => rs, throwsA(isA<PgException>()));
});
});

Expand All @@ -120,17 +119,13 @@ void main() {
final rs = conn.runTx((tx) async {
started.complete();
await runLongQuery(tx);
})
// Ignore async error, it will fail when the connection is closed and it tries to do COMMIT
// TODO: remove this ignore
.ignore();
});
// let it start
await started.future;
await Future.delayed(const Duration(milliseconds: 100));
await expectConn1ClosesForcefully(conn);

// TODO: this should be throwing PgException
await expectLater(() => rs, isNotNull);
await expectLater(() => rs, throwsA(isA<PgException>()));
});
});

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

// TODO: this should be throwing PgException
await expectLater(() => rs, isNotNull);
await expectLater(() => rs, throwsA(isA<PgException>()));
});
});
});
Expand Down
Loading