Alexey Borzenkov avatar Alexey Borzenkov committed e1550ce

Fix random VC90 crashes

Crashes appear to be random and look like objects or stack corruption.
Instead of marking ts_self volatile, fix the use of ts_current in
green_switch and green_throw, at least it seems to be working now.
If problems continue, marking global ts_* pointers volatile might
help even more, since it's clear to be optimization related.

Comments (0)

Files changed (1)

 		PyObject* args;
 		PyObject* kwargs;
 		PyObject* result;
-		volatile PyGreenlet* ts_self = ts_current;
+		PyGreenlet* ts_self = ts_current;
 		ts_self->stack_start = (char*) 1;  /* running */
 
 		args = ts_passaround_args;
 	PyObject* args,
 	PyObject* kwargs)
 {
+	PyGreenlet* current;
 	PyObject* result;
+	if (!STATE_OK)
+		return NULL;
 	Py_INCREF(args);
 	Py_XINCREF(kwargs);
-	if (STATE_OK)
-	{
+	current = ts_current;
 #ifdef GREENLET_USE_GC_DEBUG
-		printf("switching %p -> %p: args = %p (%d refs), kwargs = %p (%d refs)\n", ts_current, self,
-			args, args ? (int)Py_REFCNT(args) : 0,
-			kwargs, kwargs ? (int)Py_REFCNT(kwargs) : 0);
+	printf("switching %p -> %p: args = %p (%d refs), kwargs = %p (%d refs)\n", current, self,
+		args, args ? (int)Py_REFCNT(args) : 0,
+		kwargs, kwargs ? (int)Py_REFCNT(kwargs) : 0);
 #endif
-		ts_current->switch_refs[0] = args;
-		ts_current->switch_refs[1] = kwargs;
-	}
+	current->switch_refs[0] = args;
+	current->switch_refs[1] = kwargs;
 	result = single_result(g_switch(self, args, kwargs));
-	if (STATE_OK)
-	{
-		ts_current->switch_refs[0] = NULL;
-		ts_current->switch_refs[1] = NULL;
+	current->switch_refs[0] = NULL;
+	current->switch_refs[1] = NULL;
 #ifdef GREENLET_USE_GC_DEBUG
-		printf("returning %p -> %p: args = %p (%d refs), kwargs = %p (%d refs)\n", self, ts_current,
-			args, args ? (int)Py_REFCNT(args) : 0,
-			kwargs, kwargs ? (int)Py_REFCNT(kwargs) : 0);
-#endif
-	}
-#ifdef GREENLET_USE_GC_DEBUG
-	else
-		printf("returning from %p: failed to restore switch_refs!!!\n", self);
+	printf("returning %p -> %p: args = %p (%d refs), kwargs = %p (%d refs)\n", self, ts_current,
+		args, args ? (int)Py_REFCNT(args) : 0,
+		kwargs, kwargs ? (int)Py_REFCNT(kwargs) : 0);
 #endif
 	return result;
 }
 static PyObject *
 green_throw(PyGreenlet *self, PyObject *args)
 {
+	PyGreenlet *current;
 	PyObject *result;
 	PyObject *typ = PyExc_GreenletExit;
 	PyObject *val = NULL;
 		goto failed_throw;
 	}
 
-	if (STATE_OK)
-	{
-		ts_current->switch_refs[0] = args;
-	}
+	if (!STATE_OK)
+		goto failed_throw;
+	current = ts_current;
+	current->switch_refs[0] = args;
 	result = throw_greenlet(self, typ, val, tb);
-	if (STATE_OK)
-	{
-		ts_current->switch_refs[0] = NULL;
-	}
+	current->switch_refs[0] = NULL;
 	return result;
 
  failed_throw:
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.