@@ -387,11 +387,20 @@ start_connection(Parent, HelperSup, Deb, Sock) ->
387
387
last_blocked_by = none ,
388
388
last_blocked_at = never }},
389
389
try
390
- run ({? MODULE , recvloop ,
391
- [Deb , [], 0 , switch_callback (rabbit_event :init_stats_timer (
392
- State , # v1 .stats_timer ),
393
- handshake , 8 )]}),
394
- log (info , " closing AMQP connection ~p (~s )~n " , [self (), dynamic_connection_name (Name )])
390
+ case run ({? MODULE , recvloop ,
391
+ [Deb , [], 0 , switch_callback (rabbit_event :init_stats_timer (
392
+ State , # v1 .stats_timer ),
393
+ handshake , 8 )]}) of
394
+ % % connection was closed cleanly by the client
395
+ # v1 {connection = # connection {user = # user {username = Username },
396
+ vhost = VHost }} ->
397
+ log (info , " closing AMQP connection ~p (~s , vhost: '~s ', user: '~s ')~n " ,
398
+ [self (), dynamic_connection_name (Name ), VHost , Username ]);
399
+ % % just to be more defensive
400
+ _ ->
401
+ log (info , " closing AMQP connection ~p (~s )~n " ,
402
+ [self (), dynamic_connection_name (Name )])
403
+ end
395
404
catch
396
405
Ex ->
397
406
log_connection_exception (dynamic_connection_name (Name ), Ex )
@@ -413,6 +422,7 @@ start_connection(Parent, HelperSup, Deb, Sock) ->
413
422
log_connection_exception (Name , Ex ) ->
414
423
Severity = case Ex of
415
424
connection_closed_with_no_data_received -> debug ;
425
+ {connection_closed_abruptly , _ } -> warning ;
416
426
connection_closed_abruptly -> warning ;
417
427
_ -> error
418
428
end ,
@@ -422,6 +432,17 @@ log_connection_exception(Severity, Name, {heartbeat_timeout, TimeoutSec}) ->
422
432
% % Long line to avoid extra spaces and line breaks in log
423
433
log (Severity , " closing AMQP connection ~p (~s ):~n missed heartbeats from client, timeout: ~p s~n " ,
424
434
[self (), Name , TimeoutSec ]);
435
+ log_connection_exception (Severity , Name , {connection_closed_abruptly ,
436
+ # v1 {connection = # connection {user = # user {username = Username },
437
+ vhost = VHost }}}) ->
438
+ log (Severity , " closing AMQP connection ~p (~s , vhost: '~s ', user: '~s '):~n client unexpectedly closed TCP connection~n " ,
439
+ [self (), Name , VHost , Username ]);
440
+ % % when client abruptly closes connection before connection.open/authentication/authorization
441
+ % % succeeded, don't log username and vhost as 'none'
442
+ log_connection_exception (Severity , Name , {connection_closed_abruptly , _ }) ->
443
+ log (Severity , " closing AMQP connection ~p (~s ):~n client unexpectedly closed TCP connection~n " ,
444
+ [self (), Name ]);
445
+ % % old exception structure
425
446
log_connection_exception (Severity , Name , connection_closed_abruptly ) ->
426
447
log (Severity , " closing AMQP connection ~p (~s ):~n client unexpectedly closed TCP connection~n " ,
427
448
[self (), Name ]);
@@ -490,7 +511,7 @@ mainloop(Deb, Buf, BufLen, State = #v1{sock = Sock,
490
511
recvloop (Deb , [Data | Buf ], BufLen + size (Data ),
491
512
State # v1 {pending_recv = false });
492
513
closed when State # v1 .connection_state =:= closed ->
493
- ok ;
514
+ State ;
494
515
closed when CS =:= pre_init andalso Buf =:= [] ->
495
516
stop (tcp_healthcheck , State );
496
517
closed ->
@@ -506,7 +527,7 @@ mainloop(Deb, Buf, BufLen, State = #v1{sock = Sock,
506
527
? MODULE , Deb , {Buf , BufLen , State });
507
528
{other , Other } ->
508
529
case handle_other (Other , State ) of
509
- stop -> ok ;
530
+ stop -> State ;
510
531
NewState -> recvloop (Deb , Buf , BufLen , NewState )
511
532
end
512
533
end .
@@ -519,7 +540,7 @@ stop(tcp_healthcheck, State) ->
519
540
throw (connection_closed_with_no_data_received );
520
541
stop (closed , State ) ->
521
542
maybe_emit_stats (State ),
522
- throw (connection_closed_abruptly );
543
+ throw ({ connection_closed_abruptly , State } );
523
544
stop (Reason , State ) ->
524
545
maybe_emit_stats (State ),
525
546
throw ({inet_error , Reason }).
@@ -1129,7 +1150,7 @@ handle_method0(MethodName, FieldsBin,
1129
1150
State )
1130
1151
catch throw :{inet_error , E } when E =:= closed ; E =:= enotconn ->
1131
1152
maybe_emit_stats (State ),
1132
- throw (connection_closed_abruptly );
1153
+ throw ({ connection_closed_abruptly , State } );
1133
1154
exit :# amqp_error {method = none } = Reason ->
1134
1155
handle_exception (State , 0 , Reason # amqp_error {method = MethodName });
1135
1156
Type :Reason ->
0 commit comments