summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRyan Dahl <ry@tinyclouds.org>2010-11-16 16:26:55 -0800
committerRyan Dahl <ry@tinyclouds.org>2010-11-16 16:26:55 -0800
commitcea3a95f9fe6faaa504542d4f03349739d08a0f3 (patch)
tree87aee528b89821940c7a9cc7b7f13a1479f0edbf /src
parentcf05257fb76ab41cccf8c54b923e93d6841b91ca (diff)
downloadnode-new-cea3a95f9fe6faaa504542d4f03349739d08a0f3.tar.gz
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
Diffstat (limited to 'src')
-rw-r--r--src/node_file.cc11
1 files changed, 11 insertions, 0 deletions
diff --git a/src/node_file.cc b/src/node_file.cc
index 2a5be60b66..0deca4160d 100644
--- a/src/node_file.cc
+++ b/src/node_file.cc
@@ -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.
@@ -638,6 +639,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_obj);
+
ASYNC_CALL(write, cb, fd, buf, len, pos)
} else {
ssize_t written = pos < 0 ? write(fd, buf, len) : pwrite(fd, buf, len, pos);
@@ -702,6 +707,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_obj);
+
ASYNC_CALL(read, cb, fd, buf, len, pos);
} else {
// SYNC
@@ -792,6 +802,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) {