Skip to content

Conversation

thalesmg
Copy link
Contributor

@thalesmg thalesmg commented Sep 5, 2025

@thalesmg thalesmg force-pushed the 20250905-113-port-tls-fixes branch 4 times, most recently from fd757b4 to 1af3cbf Compare September 5, 2025 12:53
thalesmg and others added 3 commits September 5, 2025 10:24
`emqx_sock` defined an internal record for storing both the tcp and
ssl socket ports.  However, when we have a TLS error such as
`bad_certificate`, `ssl` sends a message containing the inner SSL
socket, not this internal emqtt record, and there were missing clauses
to handle that.

This caused `emqtt` to:
i) Hang indefinitely if `connect_timeout` is not specified;
ii) Fail with `{error, connack_timeout}` if `connect_timeout` is a finite value.
Example error: https://github.com/emqx/emqx/actions/runs/13924497502?pr=14893

```
*** System report during emqx_crl_cache_SUITE:t_revoked/1 2025-03-18 14:10:14.557 ***
🔗

=NOTICE REPORT==== 18-Mar-2025::14:10:14.557158 ===
TLS server: In state wait_cert at ssl_handshake.erl:2156 generated SERVER ALERT: Fatal - Certificate Revoked

*** System report during emqx_crl_cache_SUITE:t_revoked/1 2025-03-18 14:10:14.558 ***
🔗

=NOTICE REPORT==== 18-Mar-2025::14:10:14.558645 ===
TLS client: In state connection received SERVER ALERT: Fatal - Certificate Revoked

*** System report during emqx_crl_cache_SUITE:t_revoked/1 2025-03-18 14:10:14.558 ***
🔗

=DEBUG REPORT==== 18-Mar-2025::14:10:14.558841 ===
    packet: {mqtt_packet,
                {mqtt_packet_header,1,false,0,false},
                {mqtt_packet_connect,<<"MQTT">>,4,false,true,false,0,false,
                    60,#{},<<"emqtt-fc8e56a384a0-b7f1b6fd35a2c8cb9ec9">>,
                    undefined,<<>>,<<>>,undefined,<<"******">>},
                undefined}
    reason: closed
    socket: {ssl_socket,#Port<0.316>,
                {sslsocket,
                    {gen_tcp,#Port<0.316>,tls_connection,undefined},
                    [<0.143720.0>,<0.143713.0>]}}
    msg: send_data_failed
    clientid: <<"emqtt-fc8e56a384a0-b7f1b6fd35a2c8cb9ec9">>

*** System report during emqx_crl_cache_SUITE:t_revoked/1 2025-03-18 14:10:14.559 ***
🔗

=INFO REPORT==== 18-Mar-2025::14:10:14.558991 ===
    reason: closed
    msg: failed_to_send_connect_packet
    clientid: <<"emqtt-fc8e56a384a0-b7f1b6fd35a2c8cb9ec9">>

.........

*** CT Error Notification 2025-03-18 14:10:16.560 ***
🔗

emqx_crl_cache_SUITE:t_revoked failed on line 905
Reason: {case_clause,{error,connack_timeout}}
```
…_alert`)

Similar to emqx#279

This time, the error is a bit different, but the effect is the same:

```
18:17:09.437 [info] [reason: {:tls_alert, {:certificate_revoked, ~c"TLS client: In state connection received SERVER ALERT: Fatal - Certificate Revoked\n"}}, msg: ~c"failed_to_send_connect_packet", clientid: "emqtt-erlang-9e2056f5415da439bfce"]
```

```
Testing apps.emqx.emqx_crl_cache_SUITE: *** FAILED test case 1 ***
%%% emqx_crl_cache_SUITE ==> t_revoked: FAILED
%%% emqx_crl_cache_SUITE ==> {{case_clause,{error,connack_timeout}},
 [{emqx_crl_cache_SUITE,t_revoked,1,
                        [{file,"test/emqx_crl_cache_SUITE.erl"},{line,969}]},
  {test_server,ts_tc,3,[{file,"test_server.erl"},{line,1794}]},
  {test_server,run_test_case_eval1,6,[{file,"test_server.erl"},{line,1303}]},
  {test_server,run_test_case_eval,9,[{file,"test_server.erl"},{line,1235}]}]}
```

Simple to reproduce with a small sleep before `mqtt_connect`:

```diff
modified   src/emqtt.erl
@@ -1032,6 +1032,7 @@ do_connect(ConnMod, #state{pending_calls = Pendings,
                                            socket = NewSock,
                                            pending_calls = NewPendings
                                           }),
+            timer:sleep(50),
             case mqtt_connect(State3) of
                 {ok, State4} ->
                     {ok, State4};
```
@thalesmg thalesmg force-pushed the 20250905-113-port-tls-fixes branch from 1af3cbf to 279e51d Compare September 5, 2025 13:24
@thalesmg thalesmg marked this pull request as ready for review September 5, 2025 13:46
@@ -71,7 +71,7 @@ compile_emqx_test_module(M) ->
EmqttDir = code:lib_dir(emqtt),
MFilename= filename:join([EmqxDir, "test", M]),
OutDir = filename:join([EmqttDir, "test"]),
{ok, _} = compile:file(MFilename, [{outdir, OutDir}]),
{{ok, _}, M} = {compile:file(MFilename, [{outdir, OutDir}]), M},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why match M with M ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to known what caused the bad match when it happens

otherwise it's just "{badmatch, error}"

@thalesmg thalesmg merged commit 128dc0e into emqx:main-1.13 Sep 5, 2025
12 checks passed
@thalesmg thalesmg deleted the 20250905-113-port-tls-fixes branch September 5, 2025 14:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants