diff --git a/test/parallel/test-listen-fd-ebadf.js b/test/parallel/test-listen-fd-ebadf.js index dfa99e274a76e6..ffb98a9e5eb9be 100644 --- a/test/parallel/test-listen-fd-ebadf.js +++ b/test/parallel/test-listen-fd-ebadf.js @@ -1,11 +1,23 @@ 'use strict'; const common = require('../common'); + const assert = require('assert'); +const fs = require('fs'); const net = require('net'); net.createServer(common.mustNotCall()).listen({fd: 2}) .on('error', common.mustCall(onError)); -net.createServer(common.mustNotCall()).listen({fd: 42}) + +let invalidFd = 2; + +// Get first known bad file descriptor. +try { + while (fs.fstatSync(++invalidFd)); +} catch (e) { + // do nothing; we now have an invalid fd +} + +net.createServer(common.mustNotCall()).listen({ fd: invalidFd }) .on('error', common.mustCall(onError)); function onError(ex) {