<h3>Description</h3>
<p>We had Kamailio 5.1.4 with websocket module. Unfortunately, our clients don't support websocket keepalive mechanism at all, so I used TCP keepalive instead with the following parameters:</p>
<pre><code>tcp_keepalive=yes
tcp_keepcnt=6
tcp_keepidle=60
tcp_keepintvl=10
</code></pre>
<p>and set up KEEPALIVE_MECHANISM_NONE:</p>
<p><code>modparam("websocket", "keepalive_mechanism", 0)</code></p>
<p>During load testing and debugging, when 8k clients sent registrations, it was found out that shared memory was not freed after closing connections (<em>ws_connection_list_t *wsconn_used_list</em> variable in ws_conn.c ).</p>
<h3>Possible Solutions</h3>
<p>I've decided to add new keepalive mechanism that periodically checks TCP connection related to websocket:</p>
<pre><code>enum
{
    KEEPALIVE_MECHANISM_NONE = 0,
    KEEPALIVE_MECHANISM_PING = 1,
    KEEPALIVE_MECHANISM_PONG = 2,
    KEEPALIVE_MECHANISM_TCP_CONN_CHECK = 3
};  
</code></pre>
<p>and added the line to config:</p>
<pre><code># Enable custom tcp-connection-health based keepalive mechanism (3)
# KEEPALIVE_MECHANISM_NONE = 0,
# KEEPALIVE_MECHANISM_PING = 1,
# KEEPALIVE_MECHANISM_PONG = 2
# KEEPALIVE_MECHANISM_TCP_CONN_CHECK = 3
modparam("websocket", "keepalive_mechanism", 3)
</code></pre>
<p>Also, I've implemented the mechanism in ws_keepalive function:</p>
<pre><code>void ws_keepalive(unsigned int ticks, void *param)
{
    int check_time =
            (int)time(NULL) - cfg_get(websocket, ws_cfg, keepalive_timeout);

    ws_connection_t **list = NULL, **list_head = NULL;
    ws_connection_t *wsc = NULL;

    /* get an array of pointer to all ws connection */
    list_head = wsconn_get_list();
    if(!list_head)
        return;

    list = list_head;
    wsc = *list_head;
    while(wsc && wsc->last_used < check_time) {
        if (ws_keepalive_mechanism == KEEPALIVE_MECHANISM_TCP_CONN_CHECK) {
            struct tcp_connection *con = tcpconn_get(wsc->id, 0, 0, 0, 0);
            if(!con) {
                LM_INFO("tcp connection has been lost\n");
                wsc->state = WS_S_CLOSING;
            }
        }

        if(wsc->state == WS_S_CLOSING || wsc->awaiting_pong) {
            LM_INFO("forcibly closing connection\n");
            wsconn_close_now(wsc);
        } else {
            int opcode = (ws_keepalive_mechanism == KEEPALIVE_MECHANISM_PING)
                                ? OPCODE_PING
                                : OPCODE_PONG;
            ping_pong(wsc, opcode);
        }

        wsc = *(++list);
    }

    wsconn_put_list(list_head);
}

</code></pre>
<p>and changed memory allocation method in wsconn_get_list and wsconn_put_list methods from pkg to shm, because, as it turned out during load testing, using pkg_malloc (the C malloc) in this functions may cousing fails under serious loads.</p>
<p>These modifications solved the problem. But about a week ago we've started switching to ver. 5.2.1 and found a lot of changes in the websocket module. So, I've added my changes in this commit <a class="commit-link" data-hovercard-type="commit" data-hovercard-url="https://github.com/korizza/kamailio/commit/b3e03d03574ff4ff076005bb8a01d7461af2f8f5/hovercard" href="https://github.com/korizza/kamailio/commit/b3e03d03574ff4ff076005bb8a01d7461af2f8f5">korizza@<tt>b3e03d0</tt></a> . Please take a look.</p>
<h3>Additional Information</h3>
<p>Adding ws_conn_put_id in this commit <a class="commit-link" href="https://github.com/kamailio/kamailio/commit/a975bca1702ea2f3db47f834f7e4da2786ced201#diff-59c50f19ab1ccf4afe10617cdc346bc2"><tt>a975bca</tt>#diff-59c50f19ab1ccf4afe10617cdc346bc2</a> did not solve problem with ref counter increasing.</p>

<p style="font-size:small;-webkit-text-size-adjust:none;color:#666;">—<br />You are receiving this because you are subscribed to this thread.<br />Reply to this email directly, <a href="https://github.com/kamailio/kamailio/issues/1892">view it on GitHub</a>, or <a href="https://github.com/notifications/unsubscribe-auth/AF36ZdHU2JfR0hInW3cvT3OioTEwr3f8ks5vW6magaJpZM4b2jEI">mute the thread</a>.<img src="https://github.com/notifications/beacon/AF36ZTLv6h5gGwoERgyPW1-OJuWHRcpEks5vW6magaJpZM4b2jEI.gif" height="1" width="1" alt="" /></p>
<script type="application/json" data-scope="inboxmarkup">{"api_version":"1.0","publisher":{"api_key":"05dde50f1d1a384dd78767c55493e4bb","name":"GitHub"},"entity":{"external_key":"github/kamailio/kamailio","title":"kamailio/kamailio","subtitle":"GitHub repository","main_image_url":"https://github.githubassets.com/images/email/message_cards/header.png","avatar_image_url":"https://github.githubassets.com/images/email/message_cards/avatar.png","action":{"name":"Open in GitHub","url":"https://github.com/kamailio/kamailio"}},"updates":{"snippets":[{"icon":"DESCRIPTION","message":"Memory leaks in websocket module (#1892)"}],"action":{"name":"View Issue","url":"https://github.com/kamailio/kamailio/issues/1892"}}}</script>
<script type="application/ld+json">[
{
"@context": "http://schema.org",
"@type": "EmailMessage",
"potentialAction": {
"@type": "ViewAction",
"target": "https://github.com/kamailio/kamailio/issues/1892",
"url": "https://github.com/kamailio/kamailio/issues/1892",
"name": "View Issue"
},
"description": "View this Issue on GitHub",
"publisher": {
"@type": "Organization",
"name": "GitHub",
"url": "https://github.com"
}
}
]</script>