Obscure Threading Bug

Glyph Lefkowitz glyph at twistedmatrix.com
Wed Jul 19 15:32:27 EDT 2000


I apologize for the length and obscurity of the attachment.  However, the
previous example where I encountered this bug was easily twenty times as
long, and it took some effort to make it this short :-).

Simply put, the interpreter usually segfaults when I run this file.  I
don't know exactly why.  My copy of python doesn't have debugging
symbols,so my backtraces aren't terribly meaningful, but the first few
lines are usually something like:

#0  __flockfile (stream=0x0) at lockfile.c:32
#1  0x4009ac07 in _IO_ferror (fp=0x0) at ferror.c:35
#2  0x8069178 in PyFile_SetBufSize ()

the common theme being PyFile_SetBufSize ().

"lots of threads, lots of sockets" is a common theme for a lot of apps
that I would like to build, so I would be glad to help if there's any more
information I can supply.

This bug has been known to occurr on: RedHat/Debian Linux default Python
install, Digital Unix python 1.5.2 (Digital UNIX V4.0F  (Rev. 1229); Thu
Jul 15 17:56:36 EET DST 1999) and on debian using CVS python.

                      ______      __   __  _____  _     _
                     |  ____ |      \_/   |_____] |_____|
                     |_____| |_____  |    |       |     |
                     @ t w i s t e d m a t r i x  . c o m
                     http://www.twistedmatrix.com/~glyph/
-------------- next part --------------
#!/usr/bin/env python

from threading import *
from SocketServer import StreamRequestHandler, ThreadingTCPServer
from time import *
from socket import *

class Client:
	def __init__(self,host,port):
		self.connection=socket(AF_INET,SOCK_STREAM)
		self.connection.connect(host,port)
		self.rfile = self.connection.makefile('rb', 0)
		self.wfile = self.connection.makefile('wb', 0)

	def reader(self):
		try:
			while 1:
				self.rfile.read(1)
		except: pass
	def writer(self):
		try:
			while 1:
				self.wfile.write('xxxxxxxx')
		except: pass
	def read_forever(self):
		try:
			Thread(target=self.reader).start()
			Thread(target=self.writer).start()
			self.rfile.close()
			self.rfile.close()
		except: pass

class Handler(StreamRequestHandler):
	def handle(self):
		try:
			for i in range(100):
				self.rfile.read(1)
				self.wfile.write('xx')
			self.wfile.close()
			self.rfile.close()
		except: pass
		
class Server(ThreadingTCPServer):
	def __init__(self,port, handler):
		ThreadingTCPServer.__init__(self,('',port), handler)
	
	def server_bind(self):
		self.socket.setsockopt(SOL_SOCKET,SO_REUSEADDR,1)
		ThreadingTCPServer.server_bind(self)

s=Server(101010,Handler)
t=Thread(target=s.serve_forever)
t.start()
sleep(2)
for i in range(100):
	c=Client('localhost',101010)
	for i in range(100):
		Thread(target=c.read_forever).start()
		c.wfile.write('x')
		c.rfile.read()
	sleep(2)
	print 'ranging'




More information about the Python-list mailing list