Watch out for MSGQ_FREE_QUEUE in MQX 4.1

Discussion created by pbanta on Dec 1, 2014
Latest reply on Dec 4, 2014 by Radek Sestak

I found a bug in my code today that was very difficult to find.



I make use quite a few message queues for inter-task communication and synchronization.  In fact, there are 19 of them.  The code has been working without problems in MQX 3.8.1 for a long time.  I have a header file where I define the queue ids.  As my application runs through initialization quite a few queues are opened, but not all of them.  Some are opened/closed by dynamic tasks.  One of the dynamic tasks is for HTTPSRV CGI functions.  Certain CGI funtions must gather data from other parts of the system and they open/close a message queue as part of their work.  I found in my CGI function that a call to _msgq_open() was returning 0.  As I looked more closely with the debugger I could see that inside _msgq_open_internal() there was a test to see if the requested queue id was already opened.  To my surprise I saw that the queue id was, in fact, already in use.  But that's impossible!  I don't use that queue id anywhere else in my code.  But....RTCS does!!



I decided to set a breakpoint in _msgq_open_internal and what I found was when HTTPSRV is initializing it creates httpsrv_script_task which makes a call to _msgq_open(MSGQ_FREE_QUEUE, 0).  _msgq_open_internal() sees MSGQ_FREE_QUEUE as the queue id and proceeds to find the first queue id not in use and take it.  In my application it happened to be queue id 11, the same queue id I had assigned for use by my CGI tasks.


Perhaps this is covered somewhere in the docs and I just missed it.  If so, my apologies.  If not, take care when assigning message queue ids.