Fix ArrayBuffer detached error with uWebSockets.js by ruochenjia · Pull Request #5331 · socketio/socket.io · GitHub
Skip to content

Fix ArrayBuffer detached error with uWebSockets.js#5331

Open
ruochenjia wants to merge 1 commit intosocketio:mainfrom
ruochenjia:main
Open

Fix ArrayBuffer detached error with uWebSockets.js#5331
ruochenjia wants to merge 1 commit intosocketio:mainfrom
ruochenjia:main

Conversation

@ruochenjia
Copy link
Copy Markdown

The kind of change this PR does introduce

  • a bug fix
  • a new feature
  • an update to the documentation
  • a code change that improves performance
  • other

Current behavior

When using engine.io server with uWebSockets.js, the message buffer becomes detached after executing some async tasks, and throws TypeError when trying to access the buffer later on.

import uWebSocket from "uWebSockets.js";
import { uServer as Engine } from "engine.io";

const httpServer = uWebSocket.App().any("/*", (res, req) => {
	// handle regular HTTP request
}).listen("0.0.0.0", 808080, () => {
	console.log("HTTP server started!");
});

const eio = new Engine({
	transports: ["polling", "websocket"]
});

eio.on("connection", (socket) => {
	socket.on("close", () => {
		console.log("Socket closed");
	});
	socket.on("message", (buffer) => {
		console.log(buffer.toString()); // ok here

		someAsyncTask().then(() => {
			console.log(buffer.subarray(0, 3).toString()); // TypeError: Cannot perform Construct on a detached ArrayBuffer
		});
	});
});

eio.attach(httpServer);

New behavior

The original ArrayBuffer passed by the uWebSockets.js is always copied to prevent it being detached. This matches the behavior for a regular engine.io server using the built-in http module.

The documentation of uWebSockets.js states that for the message handler, the given ArrayBuffer is valid during the lifetime of this callback (until first await or return) and will be neutered.

Other information (e.g. related issues)

@darrachequesne
Copy link
Copy Markdown
Member

@ruochenjia
Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants