chriskohlhoff / asio

Asio C++ Library
http://think-async.com/Asio
4.81k stars 1.2k forks source link

asio::stream_file async_read_some always failed with "Invalid argument" #1304

Closed qicosmos closed 7 months ago

qicosmos commented 1 year ago

I have compiled and installed io_uring(https://github.com/axboe/liburing/releases/tag/liburing-2.3) on ubuntu20.04, and test stream_file according asio unit tests, but always failed.

cmake setting:

add_definitions(-DASIO_HAS_FILE)
add_definitions(-DASIO_HAS_IO_URING)
#...
target_link_libraries(test_asio_uring uring)

code:

#include <asio/io_context.hpp>
#include <asio/random_access_file.hpp>
#include <asio/stream_file.hpp>
#include <iostream>
#include <string>

using namespace asio;

void test_async_read() {
  asio::io_context ioc;
  try {
    stream_file file(ioc, "/tmp/test.txt", stream_file::read_only); // the file exists and has contents.

    char data[128]= {};
    file.async_read_some(buffer(data),
                         [data](asio::error_code ec, size_t size) {
                           std::cout << ec.message() << " " << size << "\n";
                         });
    ioc.run();
  } catch (std::exception &e) {
    std::cout << e.what() << "\n";
  }
}

int main(int, char **) {
  test_async_read();
}

Am i missed something? Anyone else meet the problem?

qicosmos commented 1 year ago

I know what's the problem, ubuntu20.04 default kernel version is 5.4, don't support io_uring. upgrade kernel version(5.10 above) , the test code pass.