Skip to content

Commit

Permalink
Work around qemu/emulator port conflicts by using `net.createConnecti…
Browse files Browse the repository at this point in the history
…on` instead of `net.createServer`
  • Loading branch information
ryanblock committed Oct 24, 2023
1 parent 3284e62 commit 464bfde
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 22 deletions.
7 changes: 6 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@

---

## [5.8.4] 2023-10-23
## [5.8.4 - 5.8.5] 2023-10-23

### Added

- Added support for hydration of platform-specific binary deps (namely: Python); fixes #1457


### Fixed

- Fixed issue where qemu/emulator port conflicts were not detected with our open port tester; fixes 1441

---

## [5.8.3] 2023-10-17
Expand Down
44 changes: 23 additions & 21 deletions src/sandbox/ports.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,36 +71,38 @@ module.exports = function getPorts (params, callback) {
*/
function checkPort (checking, ports, name, single, callback) {
let tries = 0
let tester = net.createServer()
let done = false
function check () {
if (tries === 50) {
let msg = `Could not find open port after 50 tries, please close some applications and try again`
return callback(Error(msg))
}
tester.listen(checking)
tester.once('error', err => {
if (err.message.includes('EADDRINUSE')) {
if (single) {
return callback(Error(`Port ${checking} (${name}) is already in use, please select another with prefs.arc\nSee https://arc.codes/docs/en/reference/configuration/local-preferences#ports---list for config`))
}
else {
tries++
checking++
return check()
}
let client = net.createConnection({ port: checking }, () => {
if (single) {
return callback(Error(`Port ${checking} (${name}) is already in use, please select another with prefs.arc\nSee https://arc.codes/docs/en/reference/configuration/local-preferences#ports---list for config`))
}
else {
tries++
checking++
return client.end(check)
}
})
tester.once('listening', () => {
tester.close(() => {
// Tester close emits multiple events, so only call back once
if (!done) {
done = true
if (Object.values(ports).includes(checking)) {
return callback(Error(`Port conflict found on ${checking}, please specify another port`))
client.once('error', err => {
client.end(() => {
if (err.code === 'ECONNREFUSED') {
// Tester close emits multiple events, so only call back once
if (!done) {
done = true
if (Object.values(ports).includes(checking)) {
return callback(Error(`Port conflict found on ${checking}, please specify another port`))
}
ports[name] = checking
callback()
}
ports[name] = checking
callback()
}
else {
console.error(`Unknown port checking error`)
return callback(err)
}
})
})
Expand Down

0 comments on commit 464bfde

Please sign in to comment.