Skip to content

Commit 67f9d5d

Browse files
addaleaxMylesBorins
authored andcommitted
doc: warn against concurrent http2stream.respondWithFD
Upcoming changes to move away from synchronous I/O on the main thread will imply that using the same file descriptor to respond on multiple HTTP/2 streams at the same time is invalid, because at least on Windows `uv_fs_read()` is race-y. Therefore, warn against such usage. Backport-PR-URL: #20456 PR-URL: #18762 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent b895ca9 commit 67f9d5d

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

doc/api/http2.md

+13-6
Original file line numberDiff line numberDiff line change
@@ -1262,19 +1262,19 @@ automatically.
12621262
const http2 = require('http2');
12631263
const fs = require('fs');
12641264

1265-
const fd = fs.openSync('/some/file', 'r');
1266-
12671265
const server = http2.createServer();
12681266
server.on('stream', (stream) => {
1267+
const fd = fs.openSync('/some/file', 'r');
1268+
12691269
const stat = fs.fstatSync(fd);
12701270
const headers = {
12711271
'content-length': stat.size,
12721272
'last-modified': stat.mtime.toUTCString(),
12731273
'content-type': 'text/plain'
12741274
};
12751275
stream.respondWithFD(fd, headers);
1276+
stream.on('close', () => fs.closeSync(fd));
12761277
});
1277-
server.on('close', () => fs.closeSync(fd));
12781278
```
12791279

12801280
The optional `options.statCheck` function may be specified to give user code
@@ -1287,6 +1287,12 @@ The `offset` and `length` options may be used to limit the response to a
12871287
specific range subset. This can be used, for instance, to support HTTP Range
12881288
requests.
12891289

1290+
The file descriptor is not closed when the stream is closed, so it will need
1291+
to be closed manually once it is no longer needed.
1292+
Note that using the same file descriptor concurrently for multiple streams
1293+
is not supported and may result in data loss. Re-using a file descriptor
1294+
after a stream has finished is supported.
1295+
12901296
When set, the `options.getTrailers()` function is called immediately after
12911297
queuing the last chunk of payload data to be sent. The callback is passed a
12921298
single object (with a `null` prototype) that the listener may use to specify
@@ -1296,10 +1302,10 @@ the trailing header fields to send to the peer.
12961302
const http2 = require('http2');
12971303
const fs = require('fs');
12981304

1299-
const fd = fs.openSync('/some/file', 'r');
1300-
13011305
const server = http2.createServer();
13021306
server.on('stream', (stream) => {
1307+
const fd = fs.openSync('/some/file', 'r');
1308+
13031309
const stat = fs.fstatSync(fd);
13041310
const headers = {
13051311
'content-length': stat.size,
@@ -1311,8 +1317,9 @@ server.on('stream', (stream) => {
13111317
trailers['ABC'] = 'some value to send';
13121318
}
13131319
});
1320+
1321+
stream.on('close', () => fs.closeSync(fd));
13141322
});
1315-
server.on('close', () => fs.closeSync(fd));
13161323
```
13171324

13181325
*Note*: The HTTP/1 specification forbids trailers from containing HTTP/2

0 commit comments

Comments
 (0)