0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-14 20:30:23 +00:00

Wrap the sendfile call in a try/catch for HTTP

This should reduce the amount of noise in RabbitMQ.
This commit is contained in:
Loïc Hoguin 2018-05-16 12:28:23 +02:00
parent ae6c787062
commit cc49659f93
No known key found for this signature in database
GPG key ID: 8A9DF795F6FED764

View file

@ -976,13 +976,30 @@ commands(State=#state{socket=Socket, transport=Transport, streams=Streams}, Stre
%% Send a file.
commands(State0=#state{socket=Socket, transport=Transport}, StreamID,
[{sendfile, IsFin, Offset, Bytes, Path}|Tail]) ->
%% We wrap the sendfile call into a try/catch because on OTP-20
%% and earlier a few different crashes could occur for sockets
%% that were closing or closed. For example a badarg in
%% erlang:port_get_data(#Port<...>) or a badmatch like
%% {{badmatch,{error,einval}},[{prim_file,sendfile,8,[]}...
%%
%% OTP-21 uses a NIF instead of a port so the implementation
%% and behavior has dramatically changed and it is unclear
%% whether it will be necessary in the future.
%%
%% This try/catch prevents some noisy logs to be written
%% when these errors occur.
try
Transport:sendfile(Socket, Path, Offset, Bytes),
State = case IsFin of
fin -> State0#state{out_state=done}
%% @todo Add the sendfile command.
% nofin -> State0
end,
commands(State, StreamID, Tail);
commands(State, StreamID, Tail)
catch _:_ ->
terminate(State0, {socket_error, sendfile_crash,
'An error occurred when using the sendfile function.'})
end;
%% Protocol takeover.
commands(State0=#state{ref=Ref, parent=Parent, socket=Socket, transport=Transport,
opts=Opts, children=Children}, StreamID,