stackblitz / webcontainer-core

Dev environments. In your web app.
https://webcontainers.io
MIT License
3.87k stars 157 forks source link

BUG: can't run fastify webserver [Error: EBADF: bad file descriptor, write] #54

Closed seriousme closed 1 year ago

seriousme commented 3 years ago

name: Bug report about: Create a report to help us improve title: 'can't run fastify webserver [Error: EBADF: bad file descriptor, write] ' labels: '' assignees: ''


Describe the bug can't run the fastify webserver

[nodemon] restarting due to changes...
[nodemon] starting `node server.js`
[Error: EBADF: bad file descriptor, write] {
  syscall: 'write',
  errno: -9,
  code: 'EBADF'
}
[nodemon] app crashed - waiting for file changes before starting...

Link to the blitz that caused the error https://stackblitz.com/edit/http-server-r8kujv

To Reproduce Steps to reproduce the behavior:

  1. start with the http server demo
  2. npm install fastify
  3. copy/paste quickstart example from https://fastify.io
  4. do local save

Expected behavior nodemon will start the script, fastify webserver should start at port 3000

Version of webcontainer Hash: 7286dbe8d47d3473cde7be488b7d048fb59b05f2

Screenshots If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information): Browser name = Chrome Full version = 90.0.4430.218 Major version = 90 navigator.appName = Netscape navigator.userAgent = Mozilla/5.0 (X11; CrOS x86_64 13816.82.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.218 Safari/537.36 performance.memory = { totalJSHeapSize: 53915145, usedJSHeapSize: 52589837, jsHeapSizeLimit: 2020868096 }

Additional context I know fastify is rather low level node.js, but if this works almost anything will work ;-)

KwintenP commented 3 years ago

Hi!

🙏 for reporting. This does seem like a bug on our end!

ntucker commented 3 years ago

In case this might help: having a similar issue in https://stackblitz.com/github/Rest-Hooks/todo-example

ntucker commented 3 years ago

Any progress on tracking this down?

jrvidal commented 3 years ago

@seriousme @ntucker We are starting to look into this issue now, but I can't offer a concrete timeline.

ntucker commented 2 years ago

I'm curious if the problem is finding a solution or the cause? I ask because clearly there are situations where this does work, so I'm wondering how those frameworks work around this. Is this a problem with webpack-dev-server - and using the middleware or turning off watch mode or something might help?

seriousme commented 2 years ago

I just checked: the problem seems to be in the logger:

This works: https://stackblitz.com/edit/http-server-hty4sx

With the only difference with the original being the removal of: { logger: true } from line 2.

The logger is Pino, so the problem can be reduced to (and reproduced by):

const logger = require('pino')();
logger.info('hello world');
const child = logger.child({ a: 'property' });
child.info('hello child!');

Running this code in stackblitz gives the exact same error as the original Blitz:

~/projects/http-server-hty4sx
❯ node testpino.js
[Error: EBADF: bad file descriptor, write] {
  syscall: 'write',
  errno: -9,
  code: 'EBADF'
}

Hope this helps.

seriousme commented 2 years ago

Did some more digging:

Pino uses sonic-boom to write logs, and that seems to be the source of the issue as:

const SonicBoom = require('sonic-boom');
const sonic = new SonicBoom({ fd: process.stdout.fd }); // or { dest: '/path/to/destination' }

sonic.write('hello sonic\n');

gives the exact same error:

❯ node testSonic.js
[EBADF: bad file descriptor, write] {
  code: 'EBADF',
  errno: -9,
  path: undefined,
  syscall: 'write'
}

Hope this helps.

seriousme commented 2 years ago

And digging deeper :-)

This code:

const fs= require("fs");
fs.write(1, 'hello sonic\n', 'utf8', (err, n) => {
  console.log({ err, n });
});

Returns on Linux:

$ node testSonic2.js 
hello sonic
{ err: null, n: 12 }

But on Stackblitz:

❯ node testSonic2.js
{
  err: [EBADF: bad file descriptor, write] {
    code: 'EBADF',
    errno: -9,
    path: undefined,
    syscall: 'write'
  },
  n: 0
}

This is where it ends for me and where the StackBlitz experts need to take over ;-) Workaround is of course to log to file instead ;-)

d3lm commented 1 year ago

@seriousme Thanks a lot for digging in and creating a minimal reproduction. I can confirm that this currently doesn't work. Created a project here https://stackblitz.com/edit/node-2wyyde?file=index.js.

SamVerschueren commented 1 year ago

This was fixed a while ago and just confirmed that the fastify server now works 🎉 .