Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fclose <stdin>: Bad file descriptor #953

Closed
vyskocilm opened this issue Dec 21, 2022 · 1 comment · Fixed by #954
Closed

fclose <stdin>: Bad file descriptor #953

vyskocilm opened this issue Dec 21, 2022 · 1 comment · Fixed by #954
Labels
bug Something isn't working

Comments

@vyskocilm
Copy link

Describe the bug
I experiment with userland unix in Go and your project looks like a perfect match allowing me to run a unix utilities. After I build a few of them and tested them, I have noticed the fclose <stdin>: Bad file descriptor appeared. It happens for os.Stdin, bytes.Buffer or the *io.PipeReader. The io.NopCloser has no effect there. So I believe this is nothing to do with Go, but with the wasi integration of fclose.

To Reproduce
A small C reproducer

#include <stdlib.h>
#include <stdio.h>

int main() {
    int ret = fclose(stdin);
    if (ret != 0) {
        perror("fclose <stdin>");
    }
    exit(ret);
}

To wasm - zig cc --target=wasm32-wasi fclose.c -o fclose.wasm

Run it via this program

//go:embed testdata/fclose.wasm
var fcloseWasm []byte

func main() {
	ctx := context.Background()
	if err := run(ctx); err != nil {
		log.Fatal(err)
	}
}

func run(ctx context.Context) error {
	r := wazero.NewRuntime(ctx)
	defer r.Close(ctx)

	config := wazero.NewModuleConfig().
		WithStdin(io.NopCloser(os.Stdin)).
		WithStdout(os.Stdout).
		WithStderr(os.Stderr)
	wasi_snapshot_preview1.MustInstantiate(ctx, r)

	code, err := r.CompileModule(ctx, fcloseWasm)
	if err != nil {
		return err
	}

	if _, err = r.InstantiateModule(ctx, code, config.WithArgs("wasi")); err != nil {
		if exitErr, ok := err.(*sys.ExitError); ok && exitErr.ExitCode() != 0 {
			fmt.Fprintf(os.Stderr, "exit_code: %d\n", exitErr.ExitCode())
			return err
		} else if !ok {
			return err
		}
	}
	return nil
}
go run main.go 
fclose <stdin>: Bad file descriptor
exit_code: 4294967295
2022/12/21 09:13:07 module "" closed with exit_code(4294967295)
exit status 1

Expected behavior
The stdin is closed, no error message appear and exit code is 0.

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

Environment (please complete the relevant information):

  • Go version: go1.19.1 linux/amd64
  • wazero Version: github.com/tetratelabs/wazero v1.0.0-pre.5
  • Host architecture: amd64
  • Runtime mode: compiler
@vyskocilm vyskocilm added the bug Something isn't working label Dec 21, 2022
@codefromthecrypt
Copy link
Contributor

ack this is a bug as we special case stdio in ways we shouldn't. e.g. they are pre-opened but not (yet) closable. Thanks for the report!

codefromthecrypt pushed a commit that referenced this issue Dec 21, 2022
This allows wasm to close stdio file descriptors such as STDOUT(1).
This will not close the underlying host resource as that would break a
lot of folks doing logging to the console.

Fixes #953

Signed-off-by: Adrian Cole <adrian@tetrate.io>
codefromthecrypt added a commit that referenced this issue Dec 22, 2022
Allows wasm to close stdio file descriptors

This allows wasm to close stdio file descriptors such as STDOUT(1).
This will not close the underlying host resource as that would break a
lot of folks doing logging to the console.

Fixes #953

Signed-off-by: Adrian Cole <adrian@tetrate.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants