Skip to content

Commit

Permalink
Fixed unreferencing the loop too early after calling close
Browse files Browse the repository at this point in the history
  • Loading branch information
saghul committed Nov 23, 2012
1 parent 4784e6e commit 5ef7fbf
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/handle.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ on_handle_close(uv_handle_t *handle)
Py_XDECREF(self->on_close_cb); Py_XDECREF(self->on_close_cb);
self->on_close_cb = NULL; self->on_close_cb = NULL;


Py_DECREF(self->loop);
self->loop = (Loop *)Py_None;
Py_INCREF(Py_None);

/* Refcount was increased in the caller function */ /* Refcount was increased in the caller function */
Py_DECREF(self); Py_DECREF(self);


Expand Down Expand Up @@ -80,10 +84,6 @@ Handle_func_close(Handle *self, PyObject *args)
Py_XINCREF(callback); Py_XINCREF(callback);
self->on_close_cb = callback; self->on_close_cb = callback;


Py_DECREF(self->loop);
self->loop = (Loop *)Py_None;
Py_INCREF(Py_None);

/* Increase refcount so that object is not removed before the callback is called */ /* Increase refcount so that object is not removed before the callback is called */
Py_INCREF(self); Py_INCREF(self);


Expand Down
36 changes: 36 additions & 0 deletions tests/test_tcp.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -88,6 +88,42 @@ def test_tcp1(self):
self.loop.run() self.loop.run()




class TCPTest2(unittest2.TestCase):

def setUp(self):
self.loop = pyuv.Loop.default_loop()
self.server = None
self.client = None
self.write_cb_count = 0

def on_connection(self, server, error):
self.assertEqual(error, None)
client = pyuv.TCP(pyuv.Loop.default_loop())
server.accept(client)
for x in range(1024):
client.write(b"PING"*1000, self.on_client_write)
client.close()
server.close()

def on_client_connection(self, client, error):
self.assertEqual(error, None)
self.assertTrue(client.readable)
self.assertTrue(client.writable)

def on_client_write(self, handle, error):
self.assertTrue(error in (None, pyuv.errno.UV_ECANCELED))
self.write_cb_count += 1

def test_tcp_write_cancel(self):
self.server = pyuv.TCP(self.loop)
self.server.bind(("0.0.0.0", TEST_PORT))
self.server.listen(self.on_connection)
self.client = pyuv.TCP(self.loop)
self.client.connect(("127.0.0.1", TEST_PORT), self.on_client_connection)
self.loop.run()
self.assertEqual(self.write_cb_count, 1024)


class TCPTestUnicode(unittest2.TestCase): class TCPTestUnicode(unittest2.TestCase):


def setUp(self): def setUp(self):
Expand Down

0 comments on commit 5ef7fbf

Please sign in to comment.