Skip to content

Commit

Permalink
doc: warn against concurrent http2stream.respondWithFD
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
  • Loading branch information
addaleax authored and MylesBorins committed May 2, 2018
1 parent 28a2729 commit 8b234f5
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions doc/api/http2.md
Original file line number Diff line number Diff line change
Expand Up @@ -1262,19 +1262,19 @@ automatically.
const http2 = require('http2');
const fs = require('fs');

const fd = fs.openSync('/some/file', 'r');

const server = http2.createServer();
server.on('stream', (stream) => {
const fd = fs.openSync('/some/file', 'r');

const stat = fs.fstatSync(fd);
const headers = {
'content-length': stat.size,
'last-modified': stat.mtime.toUTCString(),
'content-type': 'text/plain'
};
stream.respondWithFD(fd, headers);
stream.on('close', () => fs.closeSync(fd));
});
server.on('close', () => fs.closeSync(fd));
```

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

The file descriptor is not closed when the stream is closed, so it will need
to be closed manually once it is no longer needed.
Note that using the same file descriptor concurrently for multiple streams
is not supported and may result in data loss. Re-using a file descriptor
after a stream has finished is supported.

When set, the `options.getTrailers()` function is called immediately after
queuing the last chunk of payload data to be sent. The callback is passed a
single object (with a `null` prototype) that the listener may use to specify
Expand All @@ -1296,10 +1302,10 @@ the trailing header fields to send to the peer.
const http2 = require('http2');
const fs = require('fs');

const fd = fs.openSync('/some/file', 'r');

const server = http2.createServer();
server.on('stream', (stream) => {
const fd = fs.openSync('/some/file', 'r');

const stat = fs.fstatSync(fd);
const headers = {
'content-length': stat.size,
Expand All @@ -1311,8 +1317,9 @@ server.on('stream', (stream) => {
trailers['ABC'] = 'some value to send';
}
});

stream.on('close', () => fs.closeSync(fd));
});
server.on('close', () => fs.closeSync(fd));
```

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

0 comments on commit 8b234f5

Please sign in to comment.