#! /usr/bin/env python # # socket_crash.py -- Demonstrate a bug in socketmodule.c: internal_select() # # Requires Python 2.3, since it introduced settimeout(). # # You probably need to be root and do 'ulimit -n 4000' from your shell # immediately before running this test so you get more than the default # number of file descriptors (which is likely to be less or equal than # FD_SETSIZE). # # On my machine it crashes at fd ~ 1200. Under real world conditions you # get random crashes. If you strace it you will see corrupt arguments # to select() way before: # # select(1096, [1026 1060 1063 1065 1066 1067 1068 1069 1071 1072 1073 # 1074 1083 1091 1094 1095], NULL, NULL, {0, 1000}) = ... # import os, socket # Pre-allocate lots of file descriptors fds=[ os.open('/dev/null', os.O_RDONLY) for i in range(1000) ] while 1: # Give it a shot with a simple UDP socket s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.bind(('', 0)) print s.fileno() s.settimeout(0.001) try: s.recv(1) except socket.error: pass s.close() # What? No crash yet? So, up the ante! fds.append(os.open('/dev/null', os.O_RDONLY))