1'use strict';
2const common = require('../common');
3const assert = require('assert');
4const fs = require('fs').promises;
5
6(async () => {
7  const filehandle = await fs.open(__filename);
8
9  assert.notStrictEqual(filehandle.fd, -1);
10  await filehandle.close();
11  assert.strictEqual(filehandle.fd, -1);
12
13  // Open another file handle first. This would typically receive the fd
14  // that `filehandle` previously used. In earlier versions of Node.js, the
15  // .stat() call would then succeed because it still used the original fd;
16  // See https://github.com/nodejs/node/issues/31361 for more details.
17  const otherFilehandle = await fs.open(process.execPath);
18
19  assert.rejects(() => filehandle.stat(), {
20    code: 'EBADF',
21    syscall: 'fstat'
22  });
23
24  await otherFilehandle.close();
25})().then(common.mustCall());
26