I eventually understand what would be the issue.
Though if openssl / gnutls initializer are not called properly, that's also an issue.
Can't you run once mailstream_ssl_init_lock() at the beginning of your code to make sure that the lock is initialized properly before creating any ssl stream?