[Libwebsockets] SMP: several protocol initialization

Mykola Stryebkov mykola.stryebkov at icloud.com
Thu Apr 8 20:44:03 CEST 2021


Hi again :-)

Let’s say I use SMP server with 5+ threads and 3 protocols.  I initialise protocol->id of those protocols to 1, 2 and 3. Having that, I expect to receive exactly 3 calls to my callback with LWS_CALLBACK_PROTOCOL_INIT reason and order of initialisation is not important. Instead, in some cases (not always!) I receive:

a) more than one init call for a protocol, like 1,1,2,2,3,3. Unpleasant, but not critical: bitmap + set of mutexes solve the issue.
b) not every protocol is initialized, like 2,3,2. Critical: one of the protocols is left uninitialised.

The problem is reproducible on both win and *nix platforms.

I’m trying to reproduce the problem by modifying lws-minimal-ws-server-threads-smp with the following patch, but with no luck yet: somehow only the first protocol gets initialized.

What am I doing wrong?

=============================================
diff --git a/minimal-examples/ws-server/minimal-ws-server-threads-smp/protocol_lws_minimal.c b/minimal-examples/ws-server/minimal-ws-server-threads-smp/protocol_lws_minimal.c
index 8c085aa8..2f51fb12 100644
--- a/minimal-examples/ws-server/minimal-ws-server-threads-smp/protocol_lws_minimal.c
+++ b/minimal-examples/ws-server/minimal-ws-server-threads-smp/protocol_lws_minimal.c
@@ -158,6 +158,7 @@ callback_minimal(struct lws *wsi, enum lws_callback_reasons reason,
 
        switch (reason) {
        case LWS_CALLBACK_PROTOCOL_INIT:
+    lwsl_user("Initializing protocol %d\n", lws_get_protocol(wsi)->id);
                /* create our per-vhost struct */
                vhd = lws_protocol_vh_priv_zalloc(lws_get_vhost(wsi),
                                lws_get_protocol(wsi),
@@ -299,4 +300,18 @@ init_fail:
                sizeof(struct per_session_data__minimal), \
                128, \
                0, NULL, 0 \
-       }
+  }, \
+       { \
+               "lws-minimal1", \
+               callback_minimal, \
+               sizeof(struct per_session_data__minimal), \
+               128, \
+               1, NULL, 0 \
+  }, \
+       { \
+               "lws-minimal2", \
+               callback_minimal, \
+               sizeof(struct per_session_data__minimal), \
+               128, \
+               2, NULL, 0 \
+  }
=============================================

— 
With respect, M.
Public GnuPG key: 0x01731B47DBD56C0F
fpr: 0226 54EE C1FF 8636 36EF  2AC9 BCE9 CFC7 9CF4 6747

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://libwebsockets.org/pipermail/libwebsockets/attachments/20210408/6f06cd96/attachment.htm>


More information about the Libwebsockets mailing list