Home > Blockchain >  POSIX AIO callback called with the same (wrong) sigval repeatedly
POSIX AIO callback called with the same (wrong) sigval repeatedly

Time:09-16

I send 100 requests using aio_write. I set the sigval (callback argument) to some address using request.aio_sigevent.sigev_value.sival_ptr = some_address;. Each request has a different address. The callback is supposed to write some data to the address.

I expect the callback to be called 100 times with the 100 different arguments I gave it. Instead, the callback is called 100 times with the same argument every time: the argument for the 100th request. I've tried with both SIGEV_THREAD and SIGEV_SIGNAL with the same result both times.

The code snippet where I send the requests:

for (int i = 0; i < num_requests; i  ) {
    struct aiocb request = build_request(/* snip */, &array[i]);
    aio_write(&request);
}

(where build_request simply constructs a struct aiocb and writes to the aio fields, as well as the aio_sigevent fields: sigev_notify, sigev_signo or sigev_notify_function, and sigev_value.sival_ptr.)

Why is this happening?

CodePudding user response:

From the aio_write manual:

The control block must not be changed while the write operation is in progress.

In your case the variable goes out of scope as soon as each loop iteration ends. So it's Undefined Behaviour both in terms of pure C and in terms of the API being called. One solution is to create a static or dynamic array of aiocb.

  • Related