Skip to content

Commit cb218ca

Browse files
committed
Add suspended return type to NIF specs for dialyzer
The worker_call, worker_eval, and resume_callback NIFs can return {suspended, ...} tuples when Python code calls erlang.call() for reentrant callbacks. Updated the type specs to include this return type, fixing dialyzer warnings about unreachable pattern matches.
1 parent a325348 commit cb218ca

1 file changed

Lines changed: 13 additions & 6 deletions

File tree

src/py_nif.erl

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,24 +137,30 @@ worker_destroy(_WorkerRef) ->
137137

138138
%% @doc Call a Python function from a worker.
139139
%% This is a dirty NIF that acquires the GIL.
140+
%% May return {suspended, ...} if Python calls erlang.call() (reentrant callback).
140141
-spec worker_call(reference(), binary(), binary(), list(), map()) ->
141-
{ok, term()} | {error, term()}.
142+
{ok, term()} | {error, term()} | {suspended, term(), reference(), {binary(), term()}}.
142143
worker_call(_WorkerRef, _Module, _Func, _Args, _Kwargs) ->
143144
?NIF_STUB.
144145

145146
%% @doc Call a Python function from a worker with timeout.
147+
%% May return {suspended, ...} if Python calls erlang.call() (reentrant callback).
146148
-spec worker_call(reference(), binary(), binary(), list(), map(), non_neg_integer()) ->
147-
{ok, term()} | {error, term()}.
149+
{ok, term()} | {error, term()} | {suspended, term(), reference(), {binary(), term()}}.
148150
worker_call(_WorkerRef, _Module, _Func, _Args, _Kwargs, _TimeoutMs) ->
149151
?NIF_STUB.
150152

151153
%% @doc Evaluate a Python expression in a worker.
152-
-spec worker_eval(reference(), binary(), map()) -> {ok, term()} | {error, term()}.
154+
%% May return {suspended, ...} if Python calls erlang.call() (reentrant callback).
155+
-spec worker_eval(reference(), binary(), map()) ->
156+
{ok, term()} | {error, term()} | {suspended, term(), reference(), {binary(), term()}}.
153157
worker_eval(_WorkerRef, _Code, _Locals) ->
154158
?NIF_STUB.
155159

156160
%% @doc Evaluate a Python expression in a worker with timeout.
157-
-spec worker_eval(reference(), binary(), map(), non_neg_integer()) -> {ok, term()} | {error, term()}.
161+
%% May return {suspended, ...} if Python calls erlang.call() (reentrant callback).
162+
-spec worker_eval(reference(), binary(), map(), non_neg_integer()) ->
163+
{ok, term()} | {error, term()} | {suspended, term(), reference(), {binary(), term()}}.
158164
worker_eval(_WorkerRef, _Code, _Locals, _TimeoutMs) ->
159165
?NIF_STUB.
160166

@@ -255,8 +261,9 @@ send_callback_response(_Fd, _Response) ->
255261
%% @doc Resume a suspended Python callback with the result.
256262
%% StateRef is the reference returned in the {suspended, ...} tuple.
257263
%% Result is the callback result as a binary (status byte + data).
258-
%% Returns {ok, FinalResult} or {error, Reason}.
259-
-spec resume_callback(reference(), binary()) -> {ok, term()} | {error, term()}.
264+
%% Returns {ok, FinalResult}, {error, Reason}, or another {suspended, ...} for nested callbacks.
265+
-spec resume_callback(reference(), binary()) ->
266+
{ok, term()} | {error, term()} | {suspended, term(), reference(), {binary(), term()}}.
260267
resume_callback(_StateRef, _Result) ->
261268
?NIF_STUB.
262269

0 commit comments

Comments
 (0)