my8bird/node-leveldb — pull request #12 by justmoon (2012-02-12 16:00:23+00:00)
Title: Fix segfault in DB::Close()

In the current version of `node-leveldb` the `DB::Close()` method segfaults:

```
==10377== Thread 4:
==10377== Invalid read of size 8
==10377== at 0x56E5DC: v8::V8::DisposeGlobal(v8::internal::Object**) (in /usr/local/bin/node)
==10377== by 0x8E2307D: node_leveldb::DB::Close() (v8.h:3875)
==10377== by 0x7CBA75: eio_execute (eio.c:2283)
==10377== by 0x7CADC1: etp_proc (eio.c:2089)
==10377== by 0x61FBEFB: start_thread (pthread_create.c:304)
==10377== by 0x64F289C: clone (clone.S:112)
==10377== Address 0x950 is not stack'd, malloc'd or (recently) free'd
==10377==
==10377==
==10377== Process terminating with default action of signal 11 (SIGSEGV)
==10377== Access not within mapped region at address 0x950
==10377== at 0x56E5DC: v8::V8::DisposeGlobal(v8::internal::Object**) (in /usr/local/bin/node)
==10377== by 0x8E2307D: node_leveldb::DB::Close() (v8.h:3875)
==10377== by 0x7CBA75: eio_execute (eio.c:2283)
==10377== by 0x7CADC1: etp_proc (eio.c:2089)
==10377== by 0x61FBEFB: start_thread (pthread_create.c:304)
==10377== by 0x64F289C: clone (clone.S:112)
==10377== If you believe this happened as a result of a stack
==10377== overflow in your program's main thread (unlikely but
==10377== possible), you can try to increase the size of the
==10377== main thread stack using the --main-stacksize= flag.
==10377== The main thread stack size used in this run was 8388608.
==10377==
```

Commit cf186f64cd2b3013c1b64e32196dee5524d9ee00 introduced a regression because `V8::Handle::Dispose()` can only be called in the main thread, not asynchronously. So when `DB::Close()` tries to dispose of the Iterator handles, it segfaults.

This pull request splits the asynchronous and synchronous aspects of iterator handling into two new functions `DB::CloseIterators()` and `DB::DisposeIterators()`. This way, the iterators are closed asynchronously and then disposed of in the callback. This fixes the segfault issue.

@my8bird @mikepb