FreeRadius 3.0.3 module thread safety

Alan DeKok aland at deployingradius.com
Tue Oct 14 16:02:33 CEST 2014


Dejan Markic wrote:
> I've written a module for FreeRadius currently running on version 3.0.3. I must admit, I have a very limited understanding about the thread safety in regards to modules.

  Use local variables on the stack.  Don't use global variables.  Don't
write to the "instance" structure, or anything derived from it.

  But this advice belongs in a C programming course.

> I register module as RLM_TYPE_THREAD_SAFE and in my instantiate function I load several MySQL database instances and some other instances. So they are all in rlm_MYMODULE_t instance stuct.

  Each instance of the module gets its own "instance" structure.  They
are independent.

> Do I understand correctly, that if RLM_TYPE_THREAD_SAFE is used, this module instance struct might be used in different threads even tough I define only 4 instances, and 4 instances are created?

  The *server* is multi-threaded.  Each instance of a module can be used
by multiple threads at the same time.

  Don't confuse instances and threads.  They're different.

> So to put it in another way, I have four instances that might be used by 8 threads simultaneously?

  Yes.

> I now protect each instance with a mutex and sometimes it happens, that one instance gets  the reply from our service with a bit of delay and then that instance mutex stays locked for >0.2 second and an avalanche effect occours. I guess three instances are working normaly, but one gets slowed down with each additional request, since it waits for that same mutex. Is that correct?

  I have no idea what that means.  You're talking about internal
implementation details of your module.

  The short answer is that you should NOT be writing your own module to
talk to SQL.  This code already exists in the server.  And it's thread-safe.

  If you need to talk to SQL, see rlm_sqlippool.  It uses an existing
SQL module.  It gets connections, etc. from an existing SQL module.

  All you need to do is to call the various select functions, and
everything else is done for you automatically.

  Don't re-invent the wheel.  You're wasting your time, and you'll
likely get something wrong.

  Alan DeKok.


More information about the Freeradius-Users mailing list