Closed mdklatt closed 1 year ago
The stack trace shows that the error is originating with try_trigger_before_first_request_functions()
, which is called the config()
function here. A problem with a before_first_request
handler would explain why the bug is only observed with the first request to a new instance. But why does it only happen sometimes? Also, the config()
function is ultimately successful, because subsequent requests always succeed.
@app.before_first_request
def config():
""" Get configuration settings.
"""
# Environment variables take precedence over the config file. Config file
# keys must be ALL CAPS and at the root level.
file = Path(environ.get("HTTPEXEC_CONFIG_PATH", "etc/config.toml"))
app.config.from_file(Path.cwd() / file, load)
app.config.from_prefixed_env("HTTPEXEC")
return
Here the config()
function is not async
. The before_first_request() docs say,
if used to decorate a synchronous function, the function will be wrapped in [run_sync()] (https://quart.palletsprojects.com/en/latest/reference/source/quart.utils.html#quart.utils.run_sync) and run in a thread executor (with the wrapped function returned).
So, this function should not have to be marked as async
, but this run_sync()
adapter may not play well with hypercorn
in production. Also, cannot rule this out as a Docker issue, but that seems unlikely.
In downstream testing, marking config()
as async
eliminates the coroutine errors. Without being able to duplicate the issue here, this is only circumstantial evidence that async
is necessary. However, given that there is no reason not to use async
, go ahead with this change.
There's also a before_serving handler. How is this different than before_first_request
? Is this more appropriate to use for config()
?
Replacing @before_first_request
with @before_serving
causes unit tests to fail. This may just be an artifact of the way the test environment is defined, but using async config()
with @before_first_request
is known to work here in testing, and seems to fix the downstream app issues, so just use that.
Testing with the affected downstream applications is satisfactory. Issue resolved by commit 5e4ef80cbca97d510fbb69d47b39288b827c8fe2.
A downstream app is experiencing an issue where the first request to a new httpexec instance sometimes fails. The httpexec app is being served by hypercorn inside a Docker container. This issue cannot be duplicated here in testing, so it might be a downstream problem .