Skip to content

[REGRESSION] Device APIs crash application when called after Device::close() #805

Open
@diablodale

Description

@diablodale

Commit 4b3c687 is a regression. It causes most Device APIs to crash applications with fatal errors like SIGSEGV when Device apis are called after Device::close().

I suggest that commit be reverted.

Setup

  • all platforms, os, and compilers
  • Depthai-core v2.21.1 and all later versions

Repro

I found during code review. Then confirmed with the following test case.

TEST_CASE("Use Device APIs after close") {
    constexpr auto TEST_TIMEOUT = 20s;

    // Wait for 3s to acquire a device.
    auto t1 = steady_clock::now();
    vector<dai::DeviceInfo> availableDevices;
    do {
        availableDevices = dai::Device::getAllAvailableDevices();
        this_thread::sleep_for(500ms);
    } while(availableDevices.empty() && steady_clock::now() - t1 <= 3s);
    if(availableDevices.empty()) throw std::runtime_error("No devices found");

    // Create pipeline
    dai::Pipeline pipeline;

    // Define color source and output
    auto camColor = pipeline.create<dai::node::ColorCamera>();
    auto xoutColor = pipeline.create<dai::node::XLinkOut>();
    xoutColor->setStreamName("color");
    camColor->video.link(xoutColor->input);

    // Color properties
    camColor->setBoardSocket(dai::CameraBoardSocket::RGB);
    camColor->setResolution(dai::ColorCameraProperties::SensorResolution::THE_1080_P);
    camColor->setFps(30.0f);

    // Create device
    auto device = make_shared<dai::Device>(pipeline, availableDevices[0], dai::UsbSpeed::SUPER);
    cout << "MXID: " << device->getMxId() << "\nConnected cameras: ";
    for(const auto& cam : device->getConnectedCameras()) {
        cout << cam << " ";
    }
    cout << endl;

    // Get color queue and wait for one frame
    bool ignore{};
    if(device->getOutputQueue("color")->get<dai::ImgFrame>(TEST_TIMEOUT, ignore)) {
        cout << "Device " << device->getMxId() << " message arrived\n";
    }

    // Close device
    device->close();

    // Validate Device API behaviors
    CHECK_THROWS(std::ignore = device->getMxId());
    CHECK_THROWS(device->setXLinkChunkSize(1024));
    CHECK_NOTHROW(std::ignore = device->getDeviceInfo().name);
    CHECK_NOTHROW(device->setLogOutputLevel(dai::LogLevel::WARN));
    CHECK_NOTHROW(std::ignore = device->readCalibration());
}

Results

The app crashes and/or fails multiple catch2 rules

Expected

No crash. Test case passes.

Workaround

None. 4b3c687 was hoped to fix another bug. But instead, it creates a larger bug for a larger customer audience.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions