Skip to content

fix: shutdown should wait for in flight requests#4702

Draft
mkleczek wants to merge 2 commits intoPostgREST:mainfrom
mkleczek:graceful-shutdowns
Draft

fix: shutdown should wait for in flight requests#4702
mkleczek wants to merge 2 commits intoPostgREST:mainfrom
mkleczek:graceful-shutdowns

Conversation

@mkleczek
Copy link
Collaborator

@mkleczek mkleczek commented Mar 10, 2026

DISCLAIMER:
This commit was authored entirely by a human without the assistance of LLMs.

Fixes #4689

@mkleczek mkleczek force-pushed the graceful-shutdowns branch from 8cbf39a to ed86489 Compare March 10, 2026 11:52
@mkleczek mkleczek changed the title add: Graceful shutdown fix: shutdown should wait for in flight requests Mar 10, 2026
@mkleczek mkleczek marked this pull request as ready for review March 10, 2026 11:53
@mkleczek mkleczek requested a review from steve-chavez March 10, 2026 11:54
@mkleczek mkleczek force-pushed the graceful-shutdowns branch 4 times, most recently from 91774ab to 6972cc4 Compare March 10, 2026 14:53
Comment on lines +108 to +127
@pytest.mark.xfail(reason="Graceful shutdown is currently failing")
def test_graceful_shutdown_waits_for_in_flight_request(defaultenv):
"SIGTERM should allow in-flight requests to finish before exiting"

with run(env=defaultenv, wait_max_seconds=5) as postgrest:

def sleep():
response = postgrest.session.get("/rpc/sleep?seconds=3", timeout=10)
assert response.text == ""
assert response.status_code == 204

t = Thread(target=sleep)
t.start()

# Wait for the request to be in-flight before shutting down.
time.sleep(1)

postgrest.process.terminate()

t.join()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had no idea this happened, it would have been great to see it failing on CI first, then do the fix commit.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, do we know what happens to the pg connection when this happens? Does it keep running or it's also closed immediately?

Copy link
Collaborator Author

@mkleczek mkleczek Mar 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, do we know what happens to the pg connection when this happens? Does it keep running or it's also closed immediately?

It keeps running but the whole pool is destroyed very soon by bracket in CLI.runAppCommand.

EDIT: today the request handling thread is killed (ie. an asynchronous exception is thrown). Depending on when it happens it might cause tx rollback (if thrown in the middle of tx) or tx commit (if thrown right after tx).

Copy link
Collaborator Author

@mkleczek mkleczek Mar 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had no idea this happened, it would have been great to see it failing on CI first, then do the fix commit.

The test in the first commit is marked as xfail to make sure it is failing. The second commit then provides a fix.
I've opened a draft pr to demonstrate the failure - here is the test result: https://github.com/PostgREST/postgrest/actions/runs/22969464771/job/66681946995?pr=4708

Copy link
Collaborator Author

@mkleczek mkleczek Mar 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If raised a PR #4714 with the io test marked with xfail. I think it is a pretty cool idea to have a PR with failing test first and then another one with a fix.

@mkleczek mkleczek force-pushed the graceful-shutdowns branch from 6972cc4 to 2fb5020 Compare March 10, 2026 20:00
@mkleczek mkleczek marked this pull request as draft March 11, 2026 13:40
@mkleczek
Copy link
Collaborator Author

mkleczek commented Mar 11, 2026

Marked it as draft because there is a problem caused by yesodweb/wai#853.
What that means is we have to put the hard limit on gracefulShutdownTimeout otherwise warp will not shutdown at all if there is a reverse proxy keeping connections open (ie. using HTTP keep-alives).
@steve-chavez we need to decide what this limit should be. Unfortunately, warp is going to wait exactly that long if there are any keep-alive connections, so this timeout cannot be too long.

@mkleczek
Copy link
Collaborator Author

Related: yesodweb/wai#1064

@mkleczek
Copy link
Collaborator Author

@steve-chavez @wolfgangwalther - this is dependent on warp fixing yesodweb/wai#853. I've raised a draft PR yesodweb/wai#1064 but don't know if or when it will land.

First commit in this PR is a change to our Haskell overlay pointing to warp version from the above PR.

We need to decide what to do with this one.

@steve-chavez
Copy link
Member

this is dependent on warp fixing yesodweb/wai#853. I've raised a draft PR yesodweb/wai#1064 but don't know if or when it will land.

Nice, looks like the maintainer is already replying.

First commit in this PR is a change to our Haskell overlay pointing to warp version from the above PR.
We need to decide what to do with this one.

Since replies are fast I think we should aim to merge upstream and not fork/vendor for now.

@mkleczek mkleczek force-pushed the graceful-shutdowns branch from c8c75e4 to bcf3e19 Compare March 12, 2026 20:32
@mkleczek mkleczek force-pushed the graceful-shutdowns branch from bcf3e19 to 2ddd662 Compare March 12, 2026 20:35
themavik

This comment was marked as spam.

@mkleczek

This comment was marked as resolved.

@wolfgangwalther

This comment was marked as resolved.

@steve-chavez

This comment was marked as resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Graceful shutdown

4 participants