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

Flush response before shutting down #208

Merged

Conversation

dhouse-js
Copy link
Contributor

Server_connection.shutdown immediately calls Writer.close. But we should flush the response body in case the user has written something to the body and not yet flushed the body.

You could argue this is not needed because the user should be careful to flush their body before shutting down. In the structure of my program, this is hard to ensure. I expose a function to shut down the HTTP server, which calls Server_connection.shutdown on all live connections. It's hard for that function to know what the current state of all of the response handlers are.

@dhouse-js dhouse-js marked this pull request as ready for review April 29, 2021 18:59
@dpatti dpatti force-pushed the flush-response-before-shutting-down branch from 6a52341 to 55d806c Compare May 22, 2021 18:03
@dpatti dpatti changed the base branch from refactor-request-queue to master May 22, 2021 18:05
@dpatti dpatti force-pushed the flush-response-before-shutting-down branch from 55d806c to 084aef7 Compare May 22, 2021 19:57
Copy link
Collaborator

@dpatti dpatti left a comment

Choose a reason for hiding this comment

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

I rebased to fix history and added a commit:

In the parent feature, some changes to the test runtime were made, but
they weren't representative of what a normal runtime would do. This was
due to a bug that was fixed in another branch that this is based off of.
I reverted the changes to the runtime and reorganized the test to read
more clearly.

At the same time, I refactored shutdown_reader and shutdown_writer.
We were checking is_active twice in shutdown_writer, and it wasn't
clear if that was necessary. I also became curious of why we only wake
the reader/writer if there is no request. Presumably, if our actions on
the request cause it to wake the reader, then the wake call will be a
no-op anyway. I reorganized the code to both make the two
implementations more parallel and a bit more straightforward. This also
meant we could drop the extra wakes in shutdown.

t.read_unyield_hook |> Option.iter (fun f ->
t.read_unyield_hook <- None;
f ()))
if not t.stopped then
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is a little strange – if we need to change the runtime to make the test pass, I suspect that something is wrong with shutdown. I'm going to make a separate test to see what happens first.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Okay, I rebased this off of another branch that I believe fixes an issue exposed here.

@dpatti dpatti changed the base branch from master to test-server-shutdown May 22, 2021 19:59
@dpatti dpatti force-pushed the flush-response-before-shutting-down branch from 084aef7 to 55c100e Compare May 22, 2021 22:03
@dpatti
Copy link
Collaborator

dpatti commented May 22, 2021

This can also raise if we write more to the body after the shutdown. The shutdown will flush, but then the write loop will eventually realize that there is more to write and will try to flush it. We could try to close the response body inside the shutdown, but chunked responses will still fail because they try to write on the close. In fact, chunked responses already don't work for this, it just wasn't apparent because we didn't close the body. This is also related to #209.

@dpatti dpatti closed this Jun 2, 2021
@dpatti dpatti deleted the branch inhabitedtype:master June 2, 2021 21:25
@dpatti
Copy link
Collaborator

dpatti commented Jun 2, 2021

Oh, wow, it looks like when I merged the base and deleted the branch too quickly, it closed this PR automatically? That's surprising behavior.

@dpatti dpatti reopened this Jun 2, 2021
@dpatti dpatti changed the base branch from test-server-shutdown to master June 2, 2021 22:47
dhouse-js and others added 3 commits June 2, 2021 18:50
In the parent feature, some changes to the test runtime were made, but
they weren't representative of what a normal runtime would do. This was
due to a bug that was fixed in another branch that this is based off of.
I reverted the changes to the runtime and reorganized the test to read
more clearly.

At the same time, I refactored `shutdown_reader` and `shutdown_writer`.
We were checking `is_active` twice in `shutdown_writer`, and it wasn't
clear if that was necessary. I also became curious of why we only wake
the reader/writer if there is no request. Presumably, if our actions on
the request cause it to wake the reader, then the wake call will be a
no-op anyway. I reorganized the code to both make the two
implementations more parallel and a bit more straightforward. This also
meant we could drop the extra wakes in shutdown.
@dpatti dpatti force-pushed the flush-response-before-shutting-down branch from dd4ff5d to 366cfba Compare June 2, 2021 22:50
dhouse-js and others added 3 commits June 3, 2021 07:38
This is not new, I only just noticed it now. Removing the
`close_request_body` line does not break any tests. I wonder if it meant
to close the response body?
@seliopou seliopou merged commit 94ea5d6 into inhabitedtype:master Dec 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants