From 2f2dd3fd1a474d01c7bb189a73c0a96702c87ca4 Mon Sep 17 00:00:00 2001 From: Aleksey Lim Date: Fri, 19 Apr 2013 20:26:49 +0000 Subject: Reset c-ares resolver on network reconnection --- diff --git a/sugar_network/client/commands.py b/sugar_network/client/commands.py index d9c89be..a81a91c 100644 --- a/sugar_network/client/commands.py +++ b/sugar_network/client/commands.py @@ -329,8 +329,7 @@ class ClientCommands(db.CommandsProcessor, Commands, journal.Commands): while coroutine.select([monitor.fileno()], [], [], 1)[0]: monitor.read() self._node_job.kill() - # Otherwise, `socket.gethostbyname()` will return stale resolve - util.res_init() + coroutine.reset_resolver() def _remote_connect(self): diff --git a/sugar_network/toolkit/coroutine.py b/sugar_network/toolkit/coroutine.py index 0c40fac..64b7cf5 100644 --- a/sugar_network/toolkit/coroutine.py +++ b/sugar_network/toolkit/coroutine.py @@ -47,6 +47,10 @@ def shutdown(): return _group.join() +def reset_resolver(): + gevent.get_hub().resolver = None + + def socket(*args, **kwargs): import gevent.socket return gevent.socket.socket(*args, **kwargs) diff --git a/sugar_network/toolkit/util.py b/sugar_network/toolkit/util.py index 35d37d6..3c1ffc9 100644 --- a/sugar_network/toolkit/util.py +++ b/sugar_network/toolkit/util.py @@ -175,25 +175,6 @@ def readline(stream, limit=None): return bytes(line) -def res_init(): - """Reset resolving cache. - - Calling this function will enforce libc to avoid using stale resolving - cache after getting [re]connected. For example, if application process - was launched when there were no any DNS servers available, after getting - connected, call `res_init()` to reuse newly appeared DNS servers. - - """ - import ctypes - from ctypes.util import find_library - try: - lib_name = find_library('c') - libc = ctypes.CDLL(lib_name) - getattr(libc, '__res_init')(None) - except Exception: - _logger.exception('Failed to call res_init()') - - def default_route_exists(): with file('/proc/self/net/route') as f: # Skip header -- cgit v0.9.1