Skip to content

Commit

Permalink
Merge pull request #239 from qzhuyan/fix/william/stream-send-after-close
Browse files Browse the repository at this point in the history
Fix/william/stream send after close
  • Loading branch information
qzhuyan authored Dec 2, 2023
2 parents 197d620 + f220f00 commit 6346f4a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 24 deletions.
7 changes: 2 additions & 5 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,13 @@ jobs:
- ubuntu22.04
- ubuntu20.04
- ubuntu18.04
- ubuntu16.04
- debian12
- debian11
- debian10
- debian9
- amzn2023
- el7
- el8
- el9
- alpine3.15.1
- el8
- el7
runs-on: ubuntu-latest

steps:
Expand Down
36 changes: 17 additions & 19 deletions c_src/quicer_stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -652,10 +652,16 @@ send3(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
return ERROR_TUPLE_2(ATOM_BADARG);
}

if (!get_stream_handle(s_ctx))
{
return ERROR_TUPLE_2(ATOM_CLOSED);
}

QuicerStreamSendCTX *send_ctx = init_send_ctx();
if (!send_ctx)
{
return ERROR_TUPLE_2(ATOM_ERROR_NOT_ENOUGH_MEMORY);
res = ERROR_TUPLE_2(ATOM_ERROR_NOT_ENOUGH_MEMORY);
goto Exit;
}

ErlNifBinary *bin = &send_ctx->bin;
Expand All @@ -676,44 +682,36 @@ send3(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
}
else
{
destroy_send_ctx(send_ctx);
return ERROR_TUPLE_2(ATOM_BADARG);
res = ERROR_TUPLE_2(ATOM_BADARG);
goto ErrorExit;
}

ebin = enif_make_copy(send_ctx->env, ebin);
if (!(enif_inspect_iolist_as_binary(send_ctx->env, ebin, bin)
|| enif_inspect_binary(send_ctx->env, ebin, bin))
|| bin->size > UINT32_MAX)
{
destroy_send_ctx(send_ctx);
return ERROR_TUPLE_2(ATOM_BADARG);
}

enif_mutex_lock(s_ctx->lock);
if (!s_ctx->Stream)
{
res = ERROR_TUPLE_2(ATOM_CLOSED);
res = ERROR_TUPLE_2(ATOM_BADARG);
goto ErrorExit;
}

send_ctx->s_ctx = s_ctx;

HQUIC Stream = s_ctx->Stream;

//
// Allocates and builds the buffer to send over the stream.
//

send_ctx->s_ctx = s_ctx;
assert(bin->data != NULL);
send_ctx->Buffer.Buffer = (uint8_t *)bin->data;
send_ctx->Buffer.Length = (uint32_t)bin->size;
uint32_t bin_size = (uint32_t)bin->size;

assert(s_ctx->Stream);

QUIC_STATUS Status;
// note, SendBuffer as sendcontext, free the buffer while message is sent
// confirmed.
if (QUIC_FAILED(Status = MsQuic->StreamSend(
Stream, &send_ctx->Buffer, 1, sendflags, send_ctx)))
if (QUIC_FAILED(
Status = MsQuic->StreamSend(
s_ctx->Stream, &send_ctx->Buffer, 1, sendflags, send_ctx)))
{
res = ERROR_TUPLE_3(ATOM_STREAM_SEND_ERROR, ATOM_STATUS(Status));
goto ErrorExit;
Expand All @@ -727,7 +725,7 @@ send3(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
ErrorExit:
destroy_send_ctx(send_ctx);
Exit:
enif_mutex_unlock(s_ctx->lock);
put_stream_handle(s_ctx);
return res;
}

Expand Down
7 changes: 7 additions & 0 deletions src/quicer.erl
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
, new_fpbuffer/1
, update_fpbuffer/2
, defrag_fpbuffer/2
, is_set/2
]).
%% Exports for test
-export([ get_conn_rid/1
Expand Down Expand Up @@ -1230,6 +1231,12 @@ flush(QuicEventName, Handle) when is_atom(QuicEventName) ->
%% Event must come, do not timeout
end.

%% @doc Check if the bit mask is set in the integer.
-spec is_set(integer(), integer()) -> boolean().
is_set(Num, BitMask) when is_integer(Num)
andalso is_integer(BitMask) ->
(Num band BitMask) =:= BitMask.

-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").

Expand Down

0 comments on commit 6346f4a

Please sign in to comment.