Skip to content

Instantly share code, notes, and snippets.

@masdeseiscaracteres
Last active October 24, 2020 11:42
Show Gist options
  • Save masdeseiscaracteres/120d660d5cf76262dc4615f5ac4c37b2 to your computer and use it in GitHub Desktop.
Save masdeseiscaracteres/120d660d5cf76262dc4615f5ac4c37b2 to your computer and use it in GitHub Desktop.
Python script to ensure successful execution of a randomly-failing process
from subprocess import run
import logging
import sys
if __name__ == "__main__":
cmd_line = sys.argv[1:]
return_code = -1
while return_code!=0:
logging.warn('Executing command line "{}"'.format(' '.join(cmd_line)))
p = run(cmd_line)
return_code = p.returncode
if return_code:
logging.error('Execution failed with return code {}.'.format(hex(return_code)))
logging.warn('Successful execution')
# Script that fails with a segmentation fault with a pre-defined crash probability
import ctypes
def crash():
'''\
crash the Python interpreter...
'''
b = 'a'.encode('utf-8')
i = ctypes.c_char(b)
j = ctypes.pointer(i)
c = 0
while True:
j[c] = b
c += 1
j
##
from time import sleep
from random import random
CRASH_PROBABILITY = 0.8
sleep(1)
if random() < CRASH_PROBABILITY:
crash()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment