Watch out for MSGQ_FREE_QUEUE in MQX 4.1

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Watch out for MSGQ_FREE_QUEUE in MQX 4.1

483 Views
pbanta
Contributor IV

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

Background

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!!

Discovery

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.

Paul

Labels (1)
1 Reply

292 Views
RadekS
NXP Employee
NXP Employee

Thank you for your report and recommendation.

You are right, combining hardly set msgq IDs and dynamically opened msgq could cause such “issue”.

Idea: If you want avoid it please edit message.h file and change definition of MSGQ_FIRST_USER_QUEUE (default value is 8) to first value which is higher than highest number from your hardly set ID values. _msgq_open_interna() will start search free queue from MSGQ_FIRST_USER_QUEUE ID value.

Note: There is plan to rework RTCS for using LWMSGQ instead of MSGQ in the future…