Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

{deps, [
{cowboy, "2.12.0"},
{erlang_python, "1.8.1"}
{erlang_python, "2.1.0"}
]}.

{shell, [
Expand All @@ -39,7 +39,7 @@
{profiles, [
{test, [
{deps, [
{hackney, "3.0.2"},
{hackney, "3.2.1"},
{jsx, "3.1.0"}
]}
]}
Expand Down
17 changes: 0 additions & 17 deletions rebar.lock

This file was deleted.

20 changes: 9 additions & 11 deletions src/hornbeam_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ run_wsgi_optimized(Req, AppModule, AppCallable, State) ->
end.

%% @private
%% Context-aware fallback path using py:ctx_call
%% Context-aware fallback path using py:call
run_wsgi_with_context(Req, AppModule, AppCallable, PyContext, TimeoutMs, State) ->
%% Build environ options from state (for multi-app mode)
EnvOpts = case maps:get(script_name, State, undefined) of
Expand All @@ -159,8 +159,8 @@ run_wsgi_with_context(Req, AppModule, AppCallable, PyContext, TimeoutMs, State)
undefined -> Environ;
PathInfo -> Environ#{<<"PATH_INFO">> => PathInfo}
end,
py:ctx_call(PyContext, hornbeam_wsgi_runner, run_wsgi,
[AppModule, AppCallable, Environ1], #{}, TimeoutMs).
py:call(PyContext, hornbeam_wsgi_runner, run_wsgi,
[AppModule, AppCallable, Environ1], #{timeout => TimeoutMs}).

%% @private
%% Build environ dict for NIF optimization.
Expand Down Expand Up @@ -356,7 +356,7 @@ run_asgi_optimized(Req, AppModule, AppCallable, ReqBody, State) ->
end.

%% @private
%% Context-aware fallback path using py:ctx_call
%% Context-aware fallback path using py:call
run_asgi_with_context(Req, AppModule, AppCallable, ReqBody, PyContext, TimeoutMs, State) ->
%% Build scope options from state (for multi-app mode)
ScopeOpts = case maps:get(script_name, State, undefined) of
Expand All @@ -369,8 +369,8 @@ run_asgi_with_context(Req, AppModule, AppCallable, ReqBody, PyContext, TimeoutMs
undefined -> Scope;
PathInfo -> Scope#{<<"path">> => PathInfo, <<"raw_path">> => PathInfo}
end,
py:ctx_call(PyContext, hornbeam_asgi_runner, run_asgi,
[AppModule, AppCallable, Scope1, ReqBody], #{}, TimeoutMs).
py:call(PyContext, hornbeam_asgi_runner, run_asgi,
[AppModule, AppCallable, Scope1, ReqBody], #{timeout => TimeoutMs}).

%% @private
%% Bound context path - binds a worker for the request duration.
Expand All @@ -388,11 +388,9 @@ run_asgi_bound(Req, AppModule, AppCallable, ReqBody, TimeoutMs, State) ->
undefined -> Scope;
PathInfo -> Scope#{<<"path">> => PathInfo, <<"raw_path">> => PathInfo}
end,
%% Use with_context to bind a worker for the request duration
py:with_context(fun() ->
py:call(hornbeam_asgi_runner, run_asgi,
[AppModule, AppCallable, Scope1, ReqBody], #{}, TimeoutMs)
end).
%% Call ASGI runner - context routing handled automatically by py:call
py:call(hornbeam_asgi_runner, run_asgi,
[AppModule, AppCallable, Scope1, ReqBody], #{}, TimeoutMs).

%% @private
%% Build scope with atom keys for NIF optimization.
Expand Down
26 changes: 11 additions & 15 deletions src/hornbeam_lifespan.erl
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,11 @@ init(Opts) ->
{read_concurrency, true}
]),

%% Create a dedicated Python context for ASGI affinity
%% Get a Python context for ASGI affinity
%% This ensures module-level state persists across requests
PyContext = case py:bind(new) of
{ok, Ctx} -> Ctx;
_ -> undefined
PyContext = case py:contexts_started() of
true -> py:context();
false -> undefined
end,

%% Cache initial values
Expand Down Expand Up @@ -261,12 +261,8 @@ terminate(_Reason, #state{started = true, supported = true,
py_context = PyContext}) ->
%% Run shutdown on terminate
_ = run_shutdown(AppModule, AppCallable, PyContext),
%% Unbind the context
catch py:unbind(PyContext),
ok;
terminate(_Reason, #state{py_context = PyContext}) ->
%% Just unbind context if lifespan not started
catch py:unbind(PyContext),
terminate(_Reason, _State) ->
ok.

code_change(_OldVsn, State, _Extra) ->
Expand All @@ -293,10 +289,10 @@ run_startup(AppModule, AppCallable, PyContext) ->
Result = case PyContext of
undefined ->
py:call(hornbeam_lifespan_runner, startup,
[AppModule, AppCallable, TimeoutMs], #{}, TimeoutMs + 5000);
[AppModule, AppCallable, TimeoutMs], #{timeout => TimeoutMs + 5000});
Ctx ->
py:ctx_call(Ctx, hornbeam_lifespan_runner, startup,
[AppModule, AppCallable, TimeoutMs], #{}, TimeoutMs + 5000)
py:call(Ctx, hornbeam_lifespan_runner, startup,
[AppModule, AppCallable, TimeoutMs], #{timeout => TimeoutMs + 5000})
end,
case Result of
{ok, Response} ->
Expand Down Expand Up @@ -337,10 +333,10 @@ run_shutdown(AppModule, AppCallable, PyContext) ->
Result = case PyContext of
undefined ->
py:call(hornbeam_lifespan_runner, shutdown,
[AppModule, AppCallable], #{}, TimeoutMs);
[AppModule, AppCallable], #{timeout => TimeoutMs});
Ctx ->
py:ctx_call(Ctx, hornbeam_lifespan_runner, shutdown,
[AppModule, AppCallable], #{}, TimeoutMs)
py:call(Ctx, hornbeam_lifespan_runner, shutdown,
[AppModule, AppCallable], #{timeout => TimeoutMs})
end,
case Result of
{ok, Response} ->
Expand Down
Loading