We fulfill power fantasies

RSS Feed Back

Anecdote: "multithreading" bug

27.8.2019 17:26:37

Last night while working on MUTA's login server I ran into a strange-seeming bug with the following piece of code.

if (res)
   mysql_free_result(res),
event_push(&com_event_buf, &new_event, 1);

In the snippet, event_push() is intended to push an event to another thread. But the other thread appeared to not be receiving the event. I tried debugging with GDB and, of course, debug printing. I noticed that while the above snippet did not work, the below one, where I have seemingly only added a debug print statement, did.

if (res)
   mysql_free_result(res),
DEBUG_PRINTFF("num_events: %d\n", com_event_buf.num_events);
event_push(&com_event_buf, &new_event, 1);

Thinking too highly of myself as most programmers do, I of course thought: I'm doing nothing wrong here, so GCC must be at fault. I have turned optimizations off, but it's still doing something weird! I made everything from function parameters to the event buffer's members volatile and tried various other tweaks to no avail.

The next morning my own stupidity hit me as I tried to run the same code compiled with MSVC. Same bug, same working solution.

Then I noticed something. That something was a missing semi-colon, accidentally replaced by a period.