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

Fixes the asynchttpserver example some more #16599

Merged
merged 1 commit into from
Jan 7, 2021

Conversation

dom96
Copy link
Contributor

@dom96 dom96 commented Jan 5, 2021

I dislike this example a lot (busy looping for FDs to be closed is a very
poor waste of resources) but at least with these changes it's a little bit
better.

I dislike this example a lot (busy looping for FDs to be closed is a very
poor waste of resources) but at least with these changes it's a little bit
better.
while true:
if server.shouldAcceptRequest():
await server.acceptRequest(cb)
else:
# too many concurrent connections, `maxFDs` exceeded
poll()
# wait 500ms for FDs to be closed
await sleepAsync(500)
Copy link
Member

Choose a reason for hiding this comment

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

is there a way to wait until a FD is closed to avoid a forced wait?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That is the ideal indeed. There is no way to do this out of the box, you could potentially get a good approximation if you add some code into each close proc that would then complete a Future to let code awaiting that future know that the FDs have been freed. But that assumes all your FDs are created within Nim, if you call into C you're screwed.

More and more, I'm having a hard time keeping an open mind to this "improvement". I'm curious whether other languages implement something similar. I've never seen somebody work around FD limits like this so this really smells like a bad idea to me still, especially now that it's needing so many fixes.

Copy link

@aboisvert aboisvert Jan 10, 2021

Choose a reason for hiding this comment

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

I know this PR has already been merged but I figured I'd share my opinion (two cents) regardless.

IMO, the real pitfall here is trying to cope with too few FDs silently. If a system runs of out FDs (or any resource for that matter), it should somehow tell the administrator that it's operating beyond its capabilities and is no longer able to fulfill requests. (i.e. a simple log statement would help)

It's great that we're making httpserver more resilient, but in almost all occurrences this is still an undesirable situation and so should probably be communicated.

EDIT: Ooops I didn't realize this change was for the runnableExamples section, but still my comment applies to the serve() proc declared in asynchttpserver.nim, which still uses the poll() method. It seems these two should be consistent and include some logging for the out-of-FD situation. If it helps, LMK if you'd like me to submit a PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If you're interested in making a PR then I would encourage you to implement what I described below (#16599 (comment)). That is unless you disagree with it. Personally I don't think we should expose this mechanism in the way that we do at all. Asynchttpserver should just call accept and handle the errors it receives appropriately, and yes it should log when that happens.

Copy link
Member

Choose a reason for hiding this comment

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

potential solution: timotheecour#750

@timotheecour
Copy link
Member

busy looping for FDs to be closed

why is that busy looping? shouldn't poll only return when an event happened?

in any case, see #16603 which shows that there's a bigger problem, shouldAcceptRequest doesn't seem to work

@dom96
Copy link
Contributor Author

dom96 commented Jan 6, 2021

why is that busy looping? shouldn't poll only return when an event happened?

Sure, but just because an event happened doesn't mean an FD was freed.

in any case, see #16603 which shows that there's a bigger problem, shouldAcceptRequest doesn't seem to work

I'll let @Araq take a look. He should be the best to resolve this :)

@dom96
Copy link
Contributor Author

dom96 commented Jan 6, 2021

In fact, thinking about this some more. I'm thinking this should be resolved at the accept call-site. The code should detect that the FD limit is reached and just loop again.

You even get an error code for this...

image

@Araq Araq merged commit 4754806 into devel Jan 7, 2021
@Araq Araq deleted the dom96-asynchttpserver-example-fixes branch January 7, 2021 07:40
@Araq
Copy link
Member

Araq commented Jan 7, 2021

Making accept more robust is fine with me but my protection would still be useful on top of that.

@dom96
Copy link
Contributor Author

dom96 commented Jan 7, 2021

Making accept more robust is fine with me but my protection would still be useful on top of that.

How so? This shouldAcceptRequest mechanism could and should be replaced completely by this unless I'm missing something. Also it's not about making accept more robust, it's about doing better error handling in asynchttpserver specifically.

@Araq
Copy link
Member

Araq commented Jan 7, 2021

What if you know the request opens 4 files etc, then you can prevent accept from accepting it anyway. Just because accept found a single FD to do its work doesn't mean the request handler can accept the request.

@dom96
Copy link
Contributor Author

dom96 commented Jan 10, 2021

I would rather have the request handler fail too. It would even compose well with other error handling. You can easily handle these errors in the request handler too.

In addition, imagine you have other threads, now you'll need a lock around your call to shouldAcceptRequest because other threads might have opened more FDs.

mildred pushed a commit to mildred/Nim that referenced this pull request Jan 11, 2021
I dislike this example a lot (busy looping for FDs to be closed is a very
poor waste of resources) but at least with these changes it's a little bit
better.
ardek66 pushed a commit to ardek66/Nim that referenced this pull request Mar 26, 2021
I dislike this example a lot (busy looping for FDs to be closed is a very
poor waste of resources) but at least with these changes it's a little bit
better.
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.

4 participants