Patchwork [STABLE] parsers: do not cache RevlogError type (issue4451)

login
register
mail settings
Submitter Gregory Szorc
Date June 12, 2015, 9:45 p.m.
Message ID <6c0eef5e16a598e60623.1434145525@gps-mbp.local>
Download mbox | patch
Permalink /patch/9621/
State Accepted
Headers show

Comments

Gregory Szorc - June 12, 2015, 9:45 p.m.
# HG changeset patch
# User Gregory Szorc <gregory.szorc@gmail.com>
# Date 1434145439 25200
#      Fri Jun 12 14:43:59 2015 -0700
# Branch stable
# Node ID 6c0eef5e16a598e60623afcd8db0fdf7205bed1c
# Parent  7298da81f5a9f64ebbdef2b2195585a65da0f99e
parsers: do not cache RevlogError type (issue4451)

Index lookups raise RevlogError when the lookup fails. The previous
implementation was caching a reference to the RevlogError type in a
static variable. This assumed that the "mercurial.error" module was
only loaded once and there was only a single copy of it floating
around in memory. Unfortunately, in some situations - including
certain mod_wsgi configurations - this was not the case: the
"mercurial.error" module could be reloaded. It was possible for a
"RevlogError" reference from the first interpreter to be used by
a second interpreter. While the underlying thing was a
"mercurial.error.RevlogError," the object IDs were different, so
the Python code in revlog.py was failing to catch the exception! This
error has existed since the C index lookup code was implemented in
changeset e8d37b78acfb, which was first released in Mercurial 2.2 in
2012.
http://emptysqua.re/blog/python-c-extensions-and-mod-wsgi/#static-variables-are-shared
contains more details.

This patch removes the caching of the RevlogError type from the
function.

Since pretty much the entire function was refactored and the return
value of the function wasn't used, I changed the function signature
to not return anything.

For reasons unknown to me, we were calling PyErr_SetObject()
with the type of RevlogError and an instance of RevlogError. This
was equivalent to the Python code "raise RevlogError(RevlogError)".
This seemed wonky and completely unnecessary. The Python code only
cares about the type of the exception, not its contents. So I got
rid of this complexity.

This is my first Python C extension patch. Please give extra scrutiny
to it during review.
Yuya Nishihara - June 13, 2015, 2:35 a.m.
On Fri, 12 Jun 2015 14:45:25 -0700, Gregory Szorc wrote:
> # HG changeset patch
> # User Gregory Szorc <gregory.szorc@gmail.com>
> # Date 1434145439 25200
> #      Fri Jun 12 14:43:59 2015 -0700
> # Branch stable
> # Node ID 6c0eef5e16a598e60623afcd8db0fdf7205bed1c
> # Parent  7298da81f5a9f64ebbdef2b2195585a65da0f99e
> parsers: do not cache RevlogError type (issue4451)

> +	mod = PyImport_ImportModule("mercurial.error");
> +	if (mod == NULL) {
> +		goto cleanup;
>  	}
>  
> -	errobj = PyObject_CallFunction(errclass, NULL);
> -	if (errobj == NULL)
> -		return NULL;
> -	PyErr_SetObject(errclass, errobj);
> -	return errobj;
> +	dict = PyModule_GetDict(mod);
> +	if (dict == NULL) {
> +		goto cleanup;
> +	}
> +	Py_INCREF(dict);
>  
> -classfail:
> +	errclass = PyDict_GetItemString(dict, "RevlogError");
> +	if (errclass == NULL) {
> +		PyErr_SetString(PyExc_SystemError,
> +				"could not find RevlogError");
> +		goto cleanup;
> +	}
> +
> +	/* value of exception is ignored by callers */
> +	PyErr_SetString(errclass, "RevlogError");
> +
> +cleanup:
> +	Py_XDECREF(dict);
>  	Py_XDECREF(mod);

I'm not sure if we need to incref/decref the module dict, but if we want to
make sure the errclass is alive in this function, we could instead do

  errclass = PyObject_GetAttrString(mod, "RevlogError");
  ...
  Py_XDECREF(errclass);

https://docs.python.org/2/faq/extending.html#how-do-i-access-a-module-written-in-python-from-c

That said, I don't think it won't be suitable for stable.
Matt Mackall - June 15, 2015, 5:23 p.m.
On Fri, 2015-06-12 at 14:45 -0700, Gregory Szorc wrote:
> # HG changeset patch
> # User Gregory Szorc <gregory.szorc@gmail.com>
> # Date 1434145439 25200
> #      Fri Jun 12 14:43:59 2015 -0700
> # Branch stable
> # Node ID 6c0eef5e16a598e60623afcd8db0fdf7205bed1c
> # Parent  7298da81f5a9f64ebbdef2b2195585a65da0f99e
> parsers: do not cache RevlogError type (issue4451)

Queued for stable, thanks.

Patch

diff --git a/mercurial/parsers.c b/mercurial/parsers.c
--- a/mercurial/parsers.c
+++ b/mercurial/parsers.c
@@ -1482,43 +1482,36 @@  static int index_find_node(indexObject *
 		return rev;
 	return -2;
 }
 
-static PyObject *raise_revlog_error(void)
+static void raise_revlog_error(void)
 {
-	static PyObject *errclass;
-	PyObject *mod = NULL, *errobj;
+	PyObject *mod = NULL, *dict = NULL, *errclass = NULL;
 
-	if (errclass == NULL) {
-		PyObject *dict;
-
-		mod = PyImport_ImportModule("mercurial.error");
-		if (mod == NULL)
-			goto classfail;
-
-		dict = PyModule_GetDict(mod);
-		if (dict == NULL)
-			goto classfail;
-
-		errclass = PyDict_GetItemString(dict, "RevlogError");
-		if (errclass == NULL) {
-			PyErr_SetString(PyExc_SystemError,
-					"could not find RevlogError");
-			goto classfail;
-		}
-		Py_INCREF(errclass);
-		Py_DECREF(mod);
+	mod = PyImport_ImportModule("mercurial.error");
+	if (mod == NULL) {
+		goto cleanup;
 	}
 
-	errobj = PyObject_CallFunction(errclass, NULL);
-	if (errobj == NULL)
-		return NULL;
-	PyErr_SetObject(errclass, errobj);
-	return errobj;
+	dict = PyModule_GetDict(mod);
+	if (dict == NULL) {
+		goto cleanup;
+	}
+	Py_INCREF(dict);
 
-classfail:
+	errclass = PyDict_GetItemString(dict, "RevlogError");
+	if (errclass == NULL) {
+		PyErr_SetString(PyExc_SystemError,
+				"could not find RevlogError");
+		goto cleanup;
+	}
+
+	/* value of exception is ignored by callers */
+	PyErr_SetString(errclass, "RevlogError");
+
+cleanup:
+	Py_XDECREF(dict);
 	Py_XDECREF(mod);
-	return NULL;
 }
 
 static PyObject *index_getitem(indexObject *self, PyObject *value)
 {