Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle cases where signals are missed. #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion kapture/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def main(command_args):
process.send_signal(signal.SIGINT)
# Uniformly jitter the samples
time.sleep(PERIOD - interval)
interval = random.uniform(0, PERIOD)
interval = random.uniform(0.5*PERIOD, PERIOD)
time.sleep(interval)
i += 1
finally:
Expand Down
49 changes: 30 additions & 19 deletions kapture/wrap_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
import runpy
import signal
import sys
import time


LOG_PATH = 'log.txt'
PERIOD = 1


from collections import namedtuple
Expand All @@ -21,6 +23,7 @@ def __init__(self, completekey='tab', stdin=None, stdout=None, skip=None):
self.nosigint = True
self.allow_kbdint = False
signal.signal(signal.SIGINT, self.sigint_handler)
self.last_interupt_time = None

def sigint_handler(self, signum, frame):
if self.allow_kbdint:
Expand All @@ -33,6 +36,13 @@ def message(self, msg):
print(msg, file=self.stdout)

def logging_sigint_handler(self, signum, frame):
t = time.time()
if self.last_interupt_time == None:
interupts = 1
else:
interupts = int((t - self.last_interupt_time + 0.5)/PERIOD)
interupts = max(interupts, 1)
self.last_interupt_time = time.time()
stack, i = self.get_stack(sys._getframe().f_back, None)
#print >>self.stdout, "------\n", stack, "------\n"
if not os.path.exists(LOG_PATH):
Expand All @@ -43,25 +53,26 @@ def logging_sigint_handler(self, signum, frame):
log.write('log = []\n')

with open(LOG_PATH, 'a') as log:
log.write('log.append([\n')
for frame, line_no in stack:
code = frame.f_code
path = code.co_filename
func_name = code.co_name
linecache.checkcache(path)
line = linecache.getline(path, line_no, frame.f_globals)
if '__module__' in frame.f_globals:
module = frame.f_globals['__module__']
elif '__name__' in frame.f_globals:
module = frame.f_globals['__name__']
else:
module = None
# Remove any frames from debugging module.
if module not in ['pdb', 'bdb', 'intpdb']:
logframe = _Frame(path, line_no, func_name, line.strip(),
module)
log.write(' {!r},\n'.format(logframe))
log.write(' ])\n')
for _ in range(interupts):
log.write('log.append([\n')
for frame, line_no in stack:
code = frame.f_code
path = code.co_filename
func_name = code.co_name
linecache.checkcache(path)
line = linecache.getline(path, line_no, frame.f_globals)
if '__module__' in frame.f_globals:
module = frame.f_globals['__module__']
elif '__name__' in frame.f_globals:
module = frame.f_globals['__name__']
else:
module = None
# Remove any frames from debugging module.
if module not in ['pdb', 'bdb', 'intpdb']:
logframe = _Frame(path, line_no, func_name, line.strip(),
module)
log.write(' {!r},\n'.format(logframe))
log.write(' ])\n')

def _cmdloop(self):
while True:
Expand Down