Skip to content

Commit faf931a

Browse files
authored
fix: sending two errors to client when Pg shuts the connection down (#841)
- fix: don't send two errors to the client when server closes connection - fix: don't trigger ban behavior either (we would ban database if it closed the connection first)
1 parent 7c23661 commit faf931a

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

pgdog/src/backend/server.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,16 @@ impl Server {
441441
let error = ErrorResponse::from_bytes(message.to_bytes()?)?;
442442
self.schema_changed = error.code == "0A000";
443443
self.stats.error();
444+
445+
// Non-recoverable, Postgres is about to close the connection,
446+
// by no fault of ours or theirs.
447+
//
448+
// This shouldn't trigger ban behavior either, so that's why
449+
// we are setting ForceClose and not Error state.
450+
if matches!(error.severity.as_str(), "FATAL" | "PANIC") {
451+
self.stats.state(State::ForceClose);
452+
return Err(Error::ExecutionError(Box::new(error)));
453+
}
444454
}
445455
'W' => {
446456
debug!("streaming replication on [{}]", self.addr());
@@ -3388,4 +3398,31 @@ pub mod test {
33883398
assert!(!server.needs_drain());
33893399
verify_server_usable(&mut server).await;
33903400
}
3401+
3402+
#[tokio::test]
3403+
async fn test_fatal_error_sets_force_close() {
3404+
let mut server = test_server().await;
3405+
3406+
// Terminate our own backend — PostgreSQL sends a FATAL error.
3407+
server
3408+
.send(
3409+
&vec![ProtocolMessage::from(Query::new(
3410+
"SELECT pg_terminate_backend(pg_backend_pid())",
3411+
))]
3412+
.into(),
3413+
)
3414+
.await
3415+
.unwrap();
3416+
3417+
// Read until the FATAL error arrives.
3418+
let err = loop {
3419+
match server.read().await {
3420+
Ok(_) => continue,
3421+
Err(err) => break err,
3422+
}
3423+
};
3424+
assert!(matches!(err, Error::ExecutionError(_)));
3425+
assert!(server.force_close());
3426+
assert_eq!(server.stats().get_state(), State::ForceClose);
3427+
}
33913428
}

0 commit comments

Comments
 (0)