System hangs when TLSSocketWrapper::close() is called after ethernet cable is unplugged

Hi,

I’m testing a WebSocket client, noticed system will hang if I unplug ethernet cable. This was eventually tracked down to the function in the title., specifically the labelled line in the following code:

nsapi_error_t TLSSocketWrapper::close()
{
    if (!_transport) {
        return NSAPI_ERROR_NO_SOCKET;
    }

    tr_info("Closing TLS");

    int ret = 0;
    if (_handshake_completed) {
        //_transport->set_blocking(true);
        _transport->set_timeout(10000);
        ret = mbedtls_ssl_close_notify(&_ssl);   //  <-- this line hangs
        if (ret) {
            print_mbedtls_error("mbedtls_ssl_close_notify", ret);
        }
        _handshake_completed = false;
    }

    if (_close_transport) {
        int ret2 = _transport->close();
        if (!ret) {
            ret = ret2;
        }
    }

    _transport = NULL;

    return ret;
}

If set socket timeout to a finite number of milliseconds instead of unbound/blocking, I will get the following error output in console:

[ERR ][TLSW]: mbedtls_ssl_close_notify() failed: -0x6880 (-26752): SSL - Connection requires a write call

This seems to be a problem in both Mbed-OS 5 and 6. Setting timeout seems able to get around it, but I feel that might lead to memory leak or other unintended consequences.

Can somebody on this forum comment on this? I reported the issue on GitHub, so far it didn’t seem getting any attention from Mbed Team.