<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html lang="en">
<head>
<meta content="text/html; charset=US-ASCII" http-equiv="Content-Type">
<title>
GitLab
</title>

<style data-premailer="ignore" type="text/css">
a { color: #1068bf; }
</style>

<style>img {
max-width: 100%; height: auto;
}
body {
font-size: 0.875rem;
}
body {
-webkit-text-shadow: rgba(255,255,255,0.01) 0 0 1px;
}
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Noto Sans", Ubuntu, Cantarell, "Helvetica Neue", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"; font-size: inherit;
}
</style>
</head>
<body style='font-size: inherit; -webkit-text-shadow: rgba(255,255,255,0.01) 0 0 1px; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Noto Sans", Ubuntu, Cantarell, "Helvetica Neue", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji";'>
<div class="content">

<p class="details" style="font-style: italic; color: #666;">
<a href="https://gitlab.com/petermartx" style="color: #1068bf;">Pedro Marzo</a> created an issue: <a href="https://gitlab.com/gnutls/gnutls/-/issues/1401" style="color: #1068bf;">#1401</a>
</p>
<div class="md" style="color: #303030; word-wrap: break-word;">
<h2 dir="auto" style="font-size: 1.5em; font-weight: 600; padding-bottom: 0.3em; border-bottom-width: 1px; border-bottom-color: #eaeaea; border-bottom-style: solid; color: #303030; margin: 0 0 16px;" align="initial">
<a id="user-content-description-of-problem" class="anchor" href="#description-of-problem" aria-hidden="true" style="color: #1068bf; margin-top: 0; float: left; margin-left: -20px; text-decoration: none; outline: none;"></a>Description of problem:</h2>
<p dir="auto" style="color: #303030; margin: 0 0 16px;" align="initial">Implementing an https server with the libmicrohttpd library using one different thread per each https connection results in an slow but steady memory increment usage as TLS sessions are openned and closed. libmicrohttpd uses gnutls to implement the TLS layer.</p>
<p dir="auto" style="color: #303030; margin: 0 0 16px;" align="initial">Using valgrind massif tool it seems the problem is on _gnutls_rnd_init which calls wrap_nettle_rnd_init which makes a calloc for the random context.
These callocs seems to never be free as_gnutls_rnd_init allocates memory per each thread, but seems to not deallocate it when the thread ends, or at least I cannot see a way of doing that deallocation per thread.</p>
<p dir="auto" style="color: #303030; margin: 0 0 16px;" align="initial">These context are stored on variables defined per each thread, so as the number of threads using gnu tls increases the number of allocated contexts grows.</p>
<p dir="auto" style="color: #303030; margin: 0 0 16px;" align="initial">static _Thread_local void *gnutls_rnd_ctx;</p>
<p dir="auto" style="color: #303030; margin: 0 0 16px;" align="initial">static _Thread_local unsigned rnd_initialized = 0;</p>
<p dir="auto" style="color: #303030; margin: 0 0 16px;" align="initial">All contexts are deallocated when the program finally calls _gnutls_rnd_deinit, but that is only called by _gnutls_global_deinit, so libmicrohttpd never seems to call it because the https server never ends.
May be it is not a bug on gnutls but on libmicrohttpd, but I see no way to free the random context associated to a particular thread on the gnutls library.</p>
<h2 dir="auto" style="font-size: 1.5em; font-weight: 600; padding-bottom: 0.3em; border-bottom-width: 1px; border-bottom-color: #eaeaea; border-bottom-style: solid; color: #303030; margin: 24px 0 16px;" align="initial">
<a id="user-content-version-of-gnutls-used" class="anchor" href="#version-of-gnutls-used" aria-hidden="true" style="color: #1068bf; margin-top: 0; float: left; margin-left: -20px; text-decoration: none; outline: none;"></a>Version of gnutls used:</h2>
<p dir="auto" style="color: #303030; margin: 0 0 16px;" align="initial">3.6.14</p>
<h2 dir="auto" style="font-size: 1.5em; font-weight: 600; padding-bottom: 0.3em; border-bottom-width: 1px; border-bottom-color: #eaeaea; border-bottom-style: solid; color: #303030; margin: 24px 0 16px;" align="initial">
<a id="user-content-distributor-of-gnutls-eg-ubuntu-fedora-rhel" class="anchor" href="#distributor-of-gnutls-eg-ubuntu-fedora-rhel" aria-hidden="true" style="color: #1068bf; margin-top: 0; float: left; margin-left: -20px; text-decoration: none; outline: none;"></a>Distributor of gnutls (e.g., Ubuntu, Fedora, RHEL)</h2>
<p dir="auto" style="color: #303030; margin: 0 0 16px;" align="initial">Ubuntu</p>
<h2 dir="auto" style="font-size: 1.5em; font-weight: 600; padding-bottom: 0.3em; border-bottom-width: 1px; border-bottom-color: #eaeaea; border-bottom-style: solid; color: #303030; margin: 24px 0 16px;" align="initial">
<a id="user-content-how-reproducible" class="anchor" href="#how-reproducible" aria-hidden="true" style="color: #1068bf; margin-top: 0; float: left; margin-left: -20px; text-decoration: none; outline: none;"></a>How reproducible:</h2>
<p dir="auto" style="color: #303030; margin: 0 0 16px;" align="initial">Launch a libmicrohttpd daemon running on https with the flag MHD_USE_THREAD_PER_CONNECTION.
Using an script with curl perform https requests to the server.
Memory starts growing with each new thread created and destroyed by the libmicrohttpd library.</p>
<h2 dir="auto" style="font-size: 1.5em; font-weight: 600; padding-bottom: 0.3em; border-bottom-width: 1px; border-bottom-color: #eaeaea; border-bottom-style: solid; color: #303030; margin: 24px 0 16px;" align="initial">
<a id="user-content-actual-results" class="anchor" href="#actual-results" aria-hidden="true" style="color: #1068bf; margin-top: 0; float: left; margin-left: -20px; text-decoration: none; outline: none;"></a>Actual results:</h2>
<p dir="auto" style="color: #303030; margin: 0 0 16px;" align="initial">Memory usage increments due to wrap_nettle_rnd_init calloc, I attach the massif image
<a class="no-attachment-icon gfm" href="https://gitlab.com/gnutls/gnutls/uploads/ad315bd1d60400826c9a96d6da2af10b/memoryusage.png" target="_blank" rel="noopener noreferrer" data-canonical-src="/uploads/ad315bd1d60400826c9a96d6da2af10b/memoryusage.png" data-link="true" style="color: #1068bf; margin-top: 0;"><img src="https://gitlab.com/gnutls/gnutls/uploads/ad315bd1d60400826c9a96d6da2af10b/memoryusage.png" alt="memoryusage" data-canonical-src="/uploads/ad315bd1d60400826c9a96d6da2af10b/memoryusage.png" class="gfm" style="max-width: 100%; height: auto; margin: 0 0 8px;"></a></p>
<h2 dir="auto" style="font-size: 1.5em; font-weight: 600; padding-bottom: 0.3em; border-bottom-width: 1px; border-bottom-color: #eaeaea; border-bottom-style: solid; color: #303030; margin: 24px 0 16px;" align="initial">
<a id="user-content-expected-results" class="anchor" href="#expected-results" aria-hidden="true" style="color: #1068bf; margin-top: 0; float: left; margin-left: -20px; text-decoration: none; outline: none;"></a>Expected results:</h2>
<p dir="auto" style="color: #303030; margin: 0;" align="initial">Memory usage should not grow.</p>
</div>

</div>
<div class="footer" style="margin-top: 10px;">
<p style="font-size: small; color: #666;">

<br>
Reply to this email directly or <a href="https://gitlab.com/gnutls/gnutls/-/issues/1401" style="color: #1068bf;">view it on GitLab</a>.
<br>
You're receiving this email because of your account on <a target="_blank" rel="noopener noreferrer" href="https://gitlab.com" style="color: #1068bf;">gitlab.com</a>. <a href="https://gitlab.com/-/sent_notifications/5be91e7e7b49b00cf23d90e8fcf2cbd2/unsubscribe" target="_blank" rel="noopener noreferrer" style="color: #1068bf;">Unsubscribe</a> from this thread · <a href="https://gitlab.com/-/profile/notifications" target="_blank" rel="noopener noreferrer" class="mng-notif-link" style="color: #1068bf;">Manage all notifications</a> · <a href="https://gitlab.com/help" target="_blank" rel="noopener noreferrer" class="help-link" style="color: #1068bf;">Help</a>
<script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","action":{"@type":"ViewAction","name":"View Issue","url":"https://gitlab.com/gnutls/gnutls/-/issues/1401"}}</script>


</p>
</div>
</body>
</html>