-
Notifications
You must be signed in to change notification settings - Fork 28
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
not GC-safe #6
Comments
Good question. @Araq, please advise. |
Well usually we say "use a thread variable". |
Quoting Andreas Rumpf (2018-12-12 10:38:15)
Well usually we say "use a thread variable".
I see :)
My problem is that I'm not able to use asyncHttpServer in
single-threaded-async mode without needing "kludges" like thread
variables.
The name "asyncHttpServer" implies that it is using the async/future
mechanism for handling connections, and is thus thread safe to use. The
semantics are different from any of the other async<net|socket|dispatch>
mechanisms, which is kind of confusing.
I do understand that there is an intention to have it serve on different
threads in the future. What about keeping asyncHttpServer purely
async/future based so it can be used without precautions in a single
thread, and adding a new thing like threadedHttpServer for the case
where there are really threads handling the connections?
…--
:wq
^X^Cy^K^X^C^C^C^C
|
But that's exactly what asyncHttpServer does, it is single-threaded. I don't understand you. |
That's my point: if it is single threaded, why does it require a {.gcsafe.} callback function? |
AFAIK the reason is that if the callback is not marked |
Exactly. I consider this question answered. |
Very sorry to be a nuisance, and I'm taking the risk to be regarded as stupid, but I'm afraid I Async is nothing else then callback functions, so I do not understand at all where the problem with threads come in. Even with threads enabled: as long as the event queue is polled() from the proper thread, how can there ever arise any problems with shared data? As discussed on #nim with dom, here is a naive patch I tried on the asyncHttpServer lib. This removes the GC-safe requirement on the async handler callback, which allows me to write async HTTP servers with threads enabled and no
I have tried to follow the history of the .gcsafe. pragmas in Git, and I found that the first occurence was added by Araq in 2014 with the commit message "asynchttpserver compiles again" in My only guess is that the whole .gcsafe. propagation is ment to protect the user from polling the event loop from the wrong thread, could that be the original idea? |
Ok, answering my own last question for the sake of archiving and properly closing this issue: the whole point of the .gcsafe. seems to be to protect the user against the case where the event loop is polled from the "wrong" thread, as the compiler can not infer this. The price to pay is that the user has to perform precautions to tell the compiler "hush, I know what I'm doing" by adding |
|
I don't think that's the case. If that happens everything will blow up regardless gcsafety. I suspect the gcsafety restriction on asyncHttpServer callbacks comes from the times when future completion and gcsafety was not yet sorted (e.g. nim-lang/Nim#5738). Now that overall async-gcsafety restriction is removed (nim-lang/Nim#6059) I tend to think that asyncHttpServer gcsafety requirement can be relaxed as well. |
That was my guess as well, I was able to remove the gcsafe pragmas and have working code without warnings or errors. But beging a Nim newbe and having bothered Dom and Araq too many times about this (I still don't understand, can you explain again) I didn't feel like pushing it any further. |
Also: if the gcsafe restriction really applies to asyncHttpServer, should it not apply to all async? I can now simply create my own async http server without the gcsafe restriction. |
It does seem to be a good question.
This is using a default jester and httpbeast, which as you know prefers multi-threading. But, it also seems like database access (especially sqlite) will block the (single) async thread (and it's not operated in a threadpool, correct?) So, what is the most efficient way of writing a jester-backed server that writes to disk or database and blocks the event loop? |
Some of my Nim projects use asyncHttpServer, and I'm always nagged by the requirement for GC safe HTTP handlers, even when not using threads but only async.
Curious to see how this was handled in Jester I turned to your example code, just to find you have the same problem here :)
The example code from chapter 7:
Compiling with
--threads:on
turns this warning into an error, even.What would be the best way to handle this?
Thanks,
The text was updated successfully, but these errors were encountered: