Skip to content

Port not closed, process doesn't exit on OpenSuse Tumbleweed with Node v20 #2656

@toptensoftware

Description

@toptensoftware

SerialPort Version

v9 and v11 (at least)

Node Version

v20.3.1

Electron Version

n/a

Platform

OpenSUSE Tumbleweed: Linux localhost.localdomain 6.4.3-1-default #1 SMP PREEMPT_DYNAMIC Tue Jul 11 06:23:11 UTC 2023 (5ab030f) x86_64 x86_64 x86_64 GNU/Linux

Architecture

x64

Hardware or chipset of serialport

FT232RL and cp210x

What steps will reproduce the bug?

See sample program below using Serial Port v11.

First problem:

  1. Run program as shown on OpenSusu Tumbleweed with node v20 and the process never exits
  2. Removing either the data handler section or the drain section allows the process to exit
  3. Problem doesn't happen on Ubuntu with Node v20 or OpenSusu Tumbleweed with Node v18
  4. Problem happens with Serial Port v9.0.4 and v11.0.0

Second problem: (probably related), attempting to reopen the serial port after closing it fails with "[Error: Error Resource temporarily unavailable Cannot lock port]".

let { SerialPort } = require('serialport');

function open(device, baud)
{
    let serialPortOptions = {
        dataBits: 8,
        stopBits: 1,
        parity: 'none',
        baudRate: baud,
        path: device,
    };

    return new Promise((resolve, reject) => {
        let port = new SerialPort(serialPortOptions, function(err) {
            if (err)
                reject(err);
            else
                resolve(port);
        });
    });
        
}

function drain(port)
{
    // Drain port
    return new Promise((resolve, reject) => {
        port.drain((function(err) {
            if (err)
                reject(err);
            else
                resolve();
        }));
    });    
}

function close(port)
{
    return new Promise((resolve, reject) => {
        port.close(function(err) { 
            if (err)
                reject(err);
            else
                resolve();
        });
    })
}

function onDataHandler(data)
{
    console.log("On Data");
}

(async function() {

    // Open port
    console.log("Opening...");
    let port = await open("/dev/ttyUSB0", 115200);
    console.log("OK");

    // Data handler
    port.on('data', onDataHandler);
    port.off('data', onDataHandler);

    // Drain
    console.log("Draining");
    await drain(port);
    console.log("OK");

    // Close
    console.log("Closing");
    await close(port);
    console.log("OK");

    // PROBLEM 1: Process never exits.
    // Remove "data handler" or "drain" calls above, problem doesn't happen


    // PROBLEM 2: (possibly same issue) attempting to re-open the port fails with 
    // "[Error: Error Resource temporarily unavailable Cannot lock port]"

    // Open again...
    /*
    console.log("Opening (again)...");
    port = await open("/dev/ttyUSB0", 115200);
    console.log("OK");
    */

})();

What happens?

  1. Process didn't exit even though all event handlers removed, all completion callbacks invoked and all promises resolved.
  2. Port seems to be left open and can't be re-opened.

What should have happened?

  1. Process should exit
  2. Port should be able to be re-opened.

Additional information

As noted, doesn't happen on Ubuntu and doesn't happen pre-node v20.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions