shuveb / io_uring-by-example

A companion repository for the io_uring by Example article series
MIT License
372 stars 56 forks source link

Lost of in-progress I/Os upon io_uring exit #19

Open jacklee-cuhk opened 1 year ago

jacklee-cuhk commented 1 year ago

The current copy_file() does not wait for all submitted I/O's to complete before returning to main() which the latter closes the files and calls io_uring_queue_exit(). Any I/Os which are still in progress will be lost. This manifests as the output file having fewer bytes than the input file. This is tested and is repeatable in Ubuntu 20 running in a VM under VMWare Workstation, using liburing2_2.1-2build1_amd64.deb.

One way to fix this is to add the following codes before the return statement at the end of copy_file() to wait for all I/Os to complete before returning:

int num_pending = reads + writes; // # of I/O requests still pending.
for (int i=0; i<num_pending; i++) { // Wait for them to complete or else some may be discarded upon premature io_uring exit.
    io_uring_wait_cqe(ring, &cqe);
    io_uring_cqe_get_data(cqe);
    io_uring_cqe_seen(ring, cqe);
}

Cheers, Jack