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

serve segfaults with async await fetch handler #9222

Closed
simylein opened this issue Mar 3, 2024 · 0 comments · Fixed by #9230
Closed

serve segfaults with async await fetch handler #9222

simylein opened this issue Mar 3, 2024 · 0 comments · Fixed by #9230
Labels
bug Something isn't working crash An issue that could cause a crash

Comments

@simylein
Copy link
Contributor

simylein commented Mar 3, 2024

What version of Bun is running?

1.0.29+a146856d1

What platform is your computer?

Darwin 23.3.0 arm64 arm

What steps can reproduce the bug?

import { serve, Serve, Server, sleep } from 'bun';

declare global {
	// eslint-disable-next-line no-var
	var server: Server;
}

const parseBody = async (request: Request): Promise<unknown | null> => {
	if (request.body) {
		const body = await request.json();
		return body;
	} else {
		return null;
	}
};

const bootstrap = (): Server => {
	const options: Serve = {
		port: 4000,
		development: true,
		async fetch(request: Request): Promise<Response> {
			await sleep(200 + Math.random() * 100);
			const body = await parseBody(request);
			return new Response(JSON.stringify(body));
		},
	};

	if (!global.server) {
		global.server = serve(options);
	} else {
		global.server.reload(options);
	}

	return global.server;
};

bootstrap();
curl localhost:4000 -d '{"hello":"world"}'

What is the expected behavior?

It should not segfault.

What do you see instead?

It segfaults with Segmentation fault: 11

Additional information

It starts working when excluding the sleep call or the parseBody one

@simylein simylein added the bug Something isn't working label Mar 3, 2024
@Electroid Electroid added the crash An issue that could cause a crash label Mar 3, 2024
Jarred-Sumner added a commit that referenced this issue Mar 4, 2024
Jarred-Sumner added a commit that referenced this issue Mar 4, 2024
Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working crash An issue that could cause a crash
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants