Using a shared context in nc_accept_callhome()
Hello, How to use a shared context in ac_accept_callhome()? When I create a ly_ctx with ly_ctx_new() and give it the following function: while(1) { nc_accept_callhome(10000, lyctx, &session) .... } It crashes when multiple connections are simultaneously made.
If I create a separate ly_ctx and pass it to every call of nc_accept_callhome(), it works.
So my question is: can we use a shared context in nc_accept_callhome()? If yes, am I doing it the right way? Thanks for your reply.
I have somewhat improved the documentation to make it clear that yes, calling nc_accept_callhome() in multiple threads with the same ctx is a bad idea. All the server YANG modules are loaded into it (it is being modified) so yes, this is simply not possible because there can be the same YANG implemented in different revisions on each server, for example. In your use-case, it is probably best to just use NULL for this parameter and let libnetconf2 create the context as needed.
Or, if you want to save the time creating the context each time and are willing to risk some YANG conflicts (if they occur, the session will fail to be created), you can protect nc_accept_callhome() with a lock to avoid simultaneous modification of the context, which is the reason for the crash.
Hi Michalvasko, Thank you for the response. I agree with you. I've another question if I just call nc_accept_callhome(10000, NULL, &session): I am unable to accept more than 1019 connections with a simulator of callhome client. I looked into the code libyang, in the function ly_ctx_new(), there is a loop at: /* initialize thread-specific keys */ while ((pthread_key_create(&ctx->errlist_key, ly_err_free)) == EAGAIN) {}
The number of keys is limited by PTHREAD_KEYS_MAX (1024) in a single process, i.e., in man page of that function. So I got a return code EAGAIN. What can I do to get more than 1019 connections according to you?
You really need to have more than 1000 simultaneous NETCONF sessions? Or have them created in 1000 threads? It seems excessive and if you fix either of the conditions you should be fine. Otherwise you should be able to increase the pthread key limit somehow but I cannot help you with that, you would have to search for how to do that.
Imagine that at a network manager, we can have more than 1000 simultaneous netconf sessions. The limitation on the number of keys is per process not per thread (man page) and unfortunately, I found no way to change that limit in the system. Anyway, thank you for your response.
Yes, I guess having that many sessions for a large network can be common but then I would expect that all (or almost) have the same YANG module context meaning you can (safely) share the same context for all of them.
exactly, that's what I wanted to do with nc_accept_callhome(10000, ctx, &session) to use a shared context. This way also saves the memory to use. In that case, if connections are made one by one, it works fine. But when connections are made simultaneously, there is a crash just within 10 connections.
Like I said, put a lock around that nc_accept_callhome(). Theoretically, once the first session is created, all the other can be simultaneous because they should not modify the context but that is an unsafe thing to do.
Like you, I think that all devices are using the same yang models, so they can use a shared context. I already tested the case: after successfully making the first connection, then make simultaneously other connections. It still crashes. I also tested the case: load all required yang modules in advance, it crashes as well.
Well, you can use valgrind for example to find out the exact cause but it is probably correct and you should be simply using a lock in this case.