node-serialport: Resource temporarily unavailable Cannot lock port
SerialPort Version
12.0.0
Node Version
v20.8.0
Electron Version
No response
Platform
Linux calimero 6.6.3-zen1-1-zen #1 ZEN SMP PREEMPT_DYNAMIC Wed, 29 Nov 2023 00:40:39 +0000 x86_64 GNU/Linux
Architecture
x64
Hardware or chipset of serialport
FTDI
What steps will reproduce the bug?
const { SerialPort } = require('serialport')
async function sleep(delay) {
await new Promise(r => setTimeout(r, delay))
}
async function open() {
return new Promise((resolve, reject) => {
const p = new SerialPort({
path: '/dev/ttyUSB1',
baudRate: 9600
}, err => {
if (err) return reject(err)
console.log('opened')
resolve(p)
}
)})
}
async function close(port) {
return new Promise((resolve, reject) => {
port.close(err => {
if (err) return reject(err)
console.log('closed')
resolve()
})
})
}
async function test() {
let p = await open()
p.on('data', data => console.log(data.toString()))
await sleep(5000)
await close(p)
p = null
await sleep(5000)
}
async function main() {
try {
await test()
await test()
}
catch(e) {
console.log(e.message)
}
}
main()
What happens?
on the second opening an error occurs, showing that the first connection, despite having been closed, still locks the port
If I remove p.on('data' ... ) from the test, the error does not appear, which shows that whatever port is used it is held by the process indefinitely. Same problem if I replace p.on('data', ... ) by p.read( ...).
did I miss something? Someone would have any idea ?
What should have happened?
The port, once closed, should have reopened without error.
Additional information
No response
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 6
- Comments: 17
try set lock to false
Having the same problem with version 12.0.0 NodeJS 20.8.0. Simple repro script:
I discovered that by removing the
port.on('data')listener the port is able to reconnect correctly, with that event listener I always get the error above:Error Resource temporarily unavailable Cannot lock portcc @reconbot @GazHank
The problem seems to come from a nodejs dependency. Versions higher than 20.2 have the defect. I got around the difficulty by downgrading all my installations to v20.2.0