diff --git a/c_src/esqlite3_nif.c b/c_src/esqlite3_nif.c index 5ab5c32..6ac86f4 100644 --- a/c_src/esqlite3_nif.c +++ b/c_src/esqlite3_nif.c @@ -71,6 +71,8 @@ typedef struct { sqlite3_stmt *stmt; } esqlite_command; +ERL_NIF_TERM atom_esqlite3; + static ERL_NIF_TERM make_atom(ErlNifEnv *env, const char *atom_name) { @@ -570,7 +572,7 @@ push_command(ErlNifEnv *env, esqlite_connection *conn, esqlite_command *cmd) { static ERL_NIF_TERM make_answer(esqlite_command *cmd, ERL_NIF_TERM answer) { - return enif_make_tuple2(cmd->env, cmd->ref, answer); + return enif_make_tuple3(cmd->env, atom_esqlite3, cmd->ref, answer); } static void * @@ -919,6 +921,8 @@ on_load(ErlNifEnv* env, void** priv, ERL_NIF_TERM info) return -1; esqlite_statement_type = rt; + atom_esqlite3 = make_atom(env, "esqlite3"); + return 0; } diff --git a/src/esqlite3.erl b/src/esqlite3.erl index 102c663..597accc 100644 --- a/src/esqlite3.erl +++ b/src/esqlite3.erl @@ -287,9 +287,18 @@ close({connection, _Ref, Connection}, Timeout) -> %% Internal functions receive_answer(Ref, Timeout) -> + Start = os:timestamp(), receive - {Ref, Resp} -> Resp; - Other -> throw(Other) + {esqlite3, Ref, Resp} -> + Resp; + {esqlite3, _, _}=StaleAnswer -> + error_logger:warning_msg("Esqlite3: Ignoring stale answer ~p~n", [StaleAnswer]), + PassedMics = timer:now_diff(os:timestamp(), Start) div 1000, + NewTimeout = case Timeout - PassedMics of + Passed when Passed < 0 -> 0; + TO -> TO + end, + receive_answer(Ref, NewTimeout) after Timeout -> throw({error, timeout, Ref}) end.