HeapCrash / Challenges / Synchronous request and reply

Synchronous request and reply

Tasks get sensor readings from a sensor-hub task with a synchronous call: send a request, wait up to 100 ms for the answer. Under load, callers occasionally get the wrong sensor's value, and after a timeout the device sometimes crashes later in an unrelated function.

The code

Embedded C for Cortex-M. It compiles. There are 3 real bugs in it: the kind that pass a bench test and fail in the field.

typedef struct { uint8_t sensor; int32_t *result; } req_t;
QueueHandle_t req_q, reply_q;

bool sensor_get(uint8_t sensor, int32_t *out)
{
    int32_t value;
    req_t r = { sensor, &value };
    xQueueSend(req_q, &r, portMAX_DELAY);
    uint8_t done;
    if (xQueueReceive(reply_q, &done, pdMS_TO_TICKS(100)) != pdTRUE)
        return false;
    *out = value;
    return true;
}

void hub_task(void *p)
{
    for (;;) {
        req_t r;
        xQueueReceive(req_q, &r, portMAX_DELAY);
        *r.result = sensor_read_slow(r.sensor);
        uint8_t done = 1;
        xQueueSend(reply_q, &done, 0);
    }
}

What an interviewer expects

The bugs, reference fixes and the follow-up question are in the practice app, after you've tried it.

Companies that test this

Matched on the skills each company's embedded roles emphasise.

Related challenges

Flag the lines, say what breaks, then fix it and build it with clang in your browser.

Find the bugs