From 2170661260e8ca04217a4b01b88183866747429b Mon Sep 17 00:00:00 2001 From: Szymon Janc Date: Thu, 9 Jan 2014 11:31:48 +0100 Subject: [PATCH] shared: Fix clearing of IO handlers If NULL callback is passed to io_set_read/write_handler don't add watch for it and just clear struct io memebers. This was resulting in write/read_callback being call in loop due to fd being never written or read. --- src/shared/io-glib.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/shared/io-glib.c b/src/shared/io-glib.c index 725d974c5..ea84a69a9 100644 --- a/src/shared/io-glib.c +++ b/src/shared/io-glib.c @@ -145,8 +145,13 @@ bool io_set_read_handler(struct io *io, io_callback_func_t callback, if (!io) return false; - if (io->read_watch > 0) + if (io->read_watch > 0) { g_source_remove(io->read_watch); + io->read_watch = 0; + } + + if (!callback) + goto done; io->read_watch = g_io_add_watch_full(io->channel, G_PRIORITY_DEFAULT, G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL, @@ -154,6 +159,7 @@ bool io_set_read_handler(struct io *io, io_callback_func_t callback, if (io->read_watch == 0) return false; +done: io->read_callback = callback; io->read_destroy = destroy; io->read_data = user_data; @@ -197,8 +203,13 @@ bool io_set_write_handler(struct io *io, io_callback_func_t callback, if (!io) return false; - if (io->write_watch > 0) + if (io->write_watch > 0) { g_source_remove(io->write_watch); + io->write_watch = 0; + } + + if (!callback) + goto done; io->write_watch = g_io_add_watch_full(io->channel, G_PRIORITY_DEFAULT, G_IO_OUT | G_IO_HUP | G_IO_ERR | G_IO_NVAL, @@ -206,6 +217,7 @@ bool io_set_write_handler(struct io *io, io_callback_func_t callback, if (io->write_watch == 0) return false; +done: io->write_callback = callback; io->write_destroy = destroy; io->write_data = user_data; -- 2.47.3