Use monotonic() instead of wall clock time for shutdown timeout

This commit is contained in:
Kovid Goyal 2015-06-17 11:51:29 +05:30
parent 304d1a6f90
commit f4d5841f74
2 changed files with 7 additions and 5 deletions

View File

@ -6,7 +6,7 @@ from __future__ import (unicode_literals, division, absolute_import,
__license__ = 'GPL v3' __license__ = 'GPL v3'
__copyright__ = '2015, Kovid Goyal <kovid at kovidgoyal.net>' __copyright__ = '2015, Kovid Goyal <kovid at kovidgoyal.net>'
import ssl, socket, select, os, traceback, time import ssl, socket, select, os, traceback
from io import BytesIO from io import BytesIO
from Queue import Empty, Full from Queue import Empty, Full
from functools import partial from functools import partial
@ -580,7 +580,7 @@ class ServerLoop(object):
pass pass
for s, conn in tuple(self.connection_map.iteritems()): for s, conn in tuple(self.connection_map.iteritems()):
self.close(s, conn) self.close(s, conn)
wait_till = time.time() + self.opts.shutdown_timeout wait_till = monotonic() + self.opts.shutdown_timeout
for pool in (self.plugin_pool, self.pool): for pool in (self.plugin_pool, self.pool):
pool.stop(wait_till) pool.stop(wait_till)
if pool.workers: if pool.workers:

View File

@ -6,10 +6,12 @@ from __future__ import (unicode_literals, division, absolute_import,
__license__ = 'GPL v3' __license__ = 'GPL v3'
__copyright__ = '2015, Kovid Goyal <kovid at kovidgoyal.net>' __copyright__ = '2015, Kovid Goyal <kovid at kovidgoyal.net>'
import sys, time import sys
from Queue import Queue, Full from Queue import Queue, Full
from threading import Thread from threading import Thread
from calibre.utils.monotonic import monotonic
class Worker(Thread): class Worker(Thread):
daemon = True daemon = True
@ -67,7 +69,7 @@ class ThreadPool(object):
except Full: except Full:
break break
for w in self.workers: for w in self.workers:
now = time.time() now = monotonic()
if now >= wait_till: if now >= wait_till:
break break
w.join(wait_till - now) w.join(wait_till - now)
@ -113,7 +115,7 @@ class PluginPool(object):
except Exception: except Exception:
self.loop.log.exception('Failed to stop plugin: %s', self.plugin_name(w.plugin)) self.loop.log.exception('Failed to stop plugin: %s', self.plugin_name(w.plugin))
for w in self.workers: for w in self.workers:
left = wait_till - time.time() left = wait_till - monotonic()
if left > 0: if left > 0:
w.join(left) w.join(left)
else: else: