# HG changeset patch
# User Oleg Oshmyan <chortos@inbox.lv>
# Date 1307562806 -3600
# Node ID 5f9e6121161a16c3fa6bc8bd7c9ac794574c9725
# Parent  2db236d25ecdba303cb41b16a5ef7eab275d6073
Second attempt to work around unwarranted ERROR_ACCESS_DENIED on Windows

Also fixed an indentation error in win32 apparently caused by a bug
in my text editor.

diff -r 2db236d25ecd -r 5f9e6121161a upreckon/win32.py
--- a/upreckon/win32.py	Sat Jun 04 01:31:55 2011 +0100
+++ b/upreckon/win32.py	Wed Jun 08 20:53:26 2011 +0100
@@ -476,16 +476,15 @@
 		case.process = Popen(*args, **kwargs)
 	except OSError:
 		raise CannotStartTestee(sys.exc_info()[1])
-		case.time_started = clock()
-	# FIXME: how about checking for Escape hits?
-	while True:
+	case.time_started = clock()
+	try:
+		AssignProcessToJobObject(job, case.process._handle)
+	except WindowsError:
 		try:
-			AssignProcessToJobObject(job, case.process._handle)
+			if case.process.poll() is None:
+				raise CannotStartTestee(sys.exc_info()[1])
 		except WindowsError:
-			if sys.exc_info()[1].winerror == ERROR_ACCESS_DENIED:
-				time.sleep(0)
-		else:
-			break
+			raise CannotStartTestee(sys.exc_info()[1])
 	if not console_input:
 		if case.maxwalltime:
 			if (WaitForSingleObject(case.process._handle, case.maxwalltime) !=
@@ -553,14 +552,14 @@
 
 
 def kill(process):
-	while True:
+	# Give up after three attempts
+	for i in range(3):
 		try:
 			try:
 				process.terminate()
 			except AttributeError:
 				TerminateProcess(process._handle, 1)
 		except WindowsError:
-			if sys.exc_info()[1].winerror == ERROR_ACCESS_DENIED:
-				time.sleep(0)
+			time.sleep(0)
 		else:
 			break