Skip to content

Instantly share code, notes, and snippets.

@forivall
Last active March 16, 2023 17:45
Show Gist options
  • Save forivall/b47120eb0df9a6fe768e99ec15a85d53 to your computer and use it in GitHub Desktop.
Save forivall/b47120eb0df9a6fe768e99ec15a85d53 to your computer and use it in GitHub Desktop.
git-credential-manager reaper script

This is a simple background script to kill git-credential-manager when it hits the 100% cpu bug

#!/usr/bin/env python3
import datetime
import os
import psutil
import itertools
uid = os.getuid()
def should_reap(proc: psutil.Process):
if uid not in proc.uids():
return False
try:
name = proc.name()
if proc.status() != psutil.STATUS_RUNNING:
return False
except (psutil.ZombieProcess, psutil.NoSuchProcess, psutil.AccessDenied):
return False
if 'git-credential-manager' not in name:
return False
proc.cpu_percent(0)
created = datetime.datetime.fromtimestamp(proc.create_time())
delta = created - datetime.datetime.now()
return delta.seconds > 300 # 5 min
def do_reap():
reaped = False
for proc in psutil.process_iter():
reap = False
with proc.oneshot():
reap = should_reap(proc)
if reap:
try:
proc.cpu_percent()
if proc.cpu_percent(0.2) > 98:
print('killing %r...' % proc)
proc.kill()
reaped = True
except psutil.NoSuchProcess:
print('already killed %r!' % proc)
return reaped
if __name__ == '__main__':
import sys
reaped = do_reap()
if reaped and sys.stdin.isatty():
sys.stdout.write('!\033[D')
sys.stdout.flush()
if '--watch' in sys.argv:
import time
for i in itertools.count():
time.sleep(60)
if sys.stdin.isatty():
sys.stdout.write('.o0O'[i % 4] + '\033[D')
sys.stdout.flush()
reaped = do_reap()
if reaped and sys.stdin.isatty():
sys.stdout.write('!\033[D')
sys.stdout.flush()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment