mirror of
https://github.com/nodejs/node.git
synced 2026-11-27 17:52:27 -05:00
Add ref to buffer during fs.write and fs.read
There was the possibility the buffer could be GCed while the eio_req was pending. Still needs test coverage for the fs.read() problem. See: http://groups.google.com/group/nodejs/browse_thread/thread/c11f8b683f37cef
This commit is contained in:
1 parent
ba5ac87b12
commit
44fa89cda8
2 files changed
+61
No files matched your search
@@ -31,6 +31,7 @@ using namespace v8;
|
||||
ThrowException(Exception::TypeError(String::New("Bad argument")))
|
||||
static Persistent<String> encoding_symbol;
|
||||
static Persistent<String> errno_symbol;
|
||||
static Persistent<String> buf_symbol;
|
||||
|
||||
// Buffer for readlink() and other misc callers; keep this scoped at
|
||||
// file-level rather than method-level to avoid excess stack usage.
|
||||
@@ -636,6 +637,10 @@ static Handle<Value> Write(const Arguments& args) {
|
||||
Local<Value> cb = args[5];
|
||||
|
||||
if (cb->IsFunction()) {
|
||||
// Grab a reference to buffer so it isn't GCed
|
||||
Local<Object> cb_obj = cb->ToObject();
|
||||
cb_obj->Set(buf_symbol, buffer->handle_);
|
||||
|
||||
ASYNC_CALL(write, cb, fd, buf, len, pos)
|
||||
} else {
|
||||
ssize_t written = pos < 0 ? write(fd, buf, len) : pwrite(fd, buf, len, pos);
|
||||
@@ -698,6 +703,11 @@ static Handle<Value> Read(const Arguments& args) {
|
||||
cb = args[5];
|
||||
|
||||
if (cb->IsFunction()) {
|
||||
// Grab a reference to buffer so it isn't GCed
|
||||
// TODO: need test coverage
|
||||
Local<Object> cb_obj = cb->ToObject();
|
||||
cb_obj->Set(buf_symbol, buffer->handle_);
|
||||
|
||||
ASYNC_CALL(read, cb, fd, buf, len, pos);
|
||||
} else {
|
||||
// SYNC
|
||||
@@ -788,6 +798,7 @@ void File::Initialize(Handle<Object> target) {
|
||||
|
||||
errno_symbol = NODE_PSYMBOL("errno");
|
||||
encoding_symbol = NODE_PSYMBOL("node:encoding");
|
||||
buf_symbol = NODE_PSYMBOL("__buf");
|
||||
}
|
||||
|
||||
void InitFs(Handle<Object> target) {
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
var common = require('../common');
|
||||
var fs = require("fs");
|
||||
var assert = require("assert");
|
||||
var join = require('path').join;
|
||||
|
||||
var filename = join(common.tmpDir, 'out.txt');
|
||||
|
||||
try {
|
||||
fs.unlinkSync(filename);
|
||||
} catch (e) {
|
||||
// might not exist, that's okay.
|
||||
}
|
||||
|
||||
var fd = fs.openSync(filename, "w");
|
||||
|
||||
var line = "aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n";
|
||||
|
||||
var N = 10240, complete = 0;
|
||||
for (var i = 0; i < N; i ++) {
|
||||
// Create a new buffer for each write. Before the write is actually
|
||||
// executed by the thread pool, the buffer will be collected.
|
||||
var buffer = new Buffer(line);
|
||||
fs.write(fd, buffer, 0, buffer.length, null, function (er, written) {
|
||||
complete++;
|
||||
if (complete === N) {
|
||||
fs.closeSync(fd);
|
||||
var s = fs.createReadStream(filename);
|
||||
s.on("data", testBuffer);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
var bytesChecked = 0;
|
||||
|
||||
function testBuffer (b) {
|
||||
for (var i = 0; i < b.length; i++) {
|
||||
bytesChecked++;
|
||||
if (b[i] !== 'a'.charCodeAt(0) && b[i] !== '\n'.charCodeAt(0)) {
|
||||
throw new Error("invalid char "+i+","+b[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
process.on('exit', function () {
|
||||
// Probably some of the writes are going to overlap, so we can't assume
|
||||
// that we get (N * line.length). Let's just make sure we've checked a
|
||||
// few...
|
||||
assert.ok(bytesChecked > 1000);
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user