[mod_python] Segfault when using PythonImport directive

Michael C. Neel neel at mediapulse.com
Tue Oct 8 16:33:19 EST 2002

Not addressing the segfault, couldn't you handle globals in the python
program itself?  I do this for database handles my self:

from mod_python import apache
import MySQLdb

db = MySQLdb.connect("yada yada")

def handler(req):
	do cool db stuff with req here

I do this often, and I've checked that each request doesn't generate a
db connection by watching the MySQL number of connects.  One thing to
keep in mind is that a db handle may go stale if not used by some
databases, so be sure to add some code to test out the db handle each
request and reconnect if needed.  The DB API 2.0 spec doesn't define a
ping method like perl's DBI does, so I usually add one myself for this.


Michael C. Neel
Director of Software Development
Mediapulse, Inc.

-----Original Message-----
From: rmunn at pobox.com [mailto:rmunn at pobox.com] 
Sent: Tuesday, October 08, 2002 1:35 PM
To: mod_python at modpython.org
Subject: [mod_python] Segfault when using PythonImport directive

I'm using mod_python 3.0.0 BETA 2 with Apache 2.0.42 and encountering a
segfault every time I try to use the PythonInclude directive. The
segfault occurs even when I try to have Apache do a configuration syntax
check (/usr/apache2/bin/apachectl configtest). I've narrowed it down to
a single PythonInclude line. This is the <Directory> directive in which
the PythonInclude directive appears:

<Directory /var/www/publisher>
    AddHandler python-program .py
    PythonHandler mod_python.publisher
    PythonInterpPerDirective On
    PythonImport globals

If I comment out the "PythonImport globals" line, everything runs fine.

Here's the contents of my /var/www/publisher directory:

rmunn at localhost:/var/www/publisher$ ls -l
total 8
-rw-r--r--    1 rmunn    users          12 Oct  7 19:22 globals.py
-rw-r--r--    1 rmunn    users         133 Oct  8 12:26 tryme.py
rmunn at localhost:/var/www/publisher$ cat globals.py 
foo = 'bar'
rmunn at localhost:/var/www/publisher$ cat tryme.py 
def show(req, name):
        return str(globals.foo)
    except AttributeError, NameError:
        return str(dir(globals))

As you can see, what I'm trying to do is have some global variables
pre-defined in my mod_python scripts; I plan on using this to accomplish
expensive operations (like opening database connections) once per apache
process child instead of once per request.

Here's the gdb backtrace of my segfault. I'm running httpd -t to only
get a configuration syntax check and it's still failing. Any ideas?

root at localhost:/usr/apache2/conf# gdb ../bin/httpd 
GNU gdb 5.2.1
Copyright 2002 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you
welcome to change it and/or distribute copies of it under certain
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for
This GDB was configured as "i686-pc-linux-gnu"...
(gdb) run -t
Starting program: /usr/apache2/bin/httpd -t
[New Thread 1024 (LWP 29765)]

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 1024 (LWP 29765)]
__pthread_mutex_lock (mutex=0x10) at mutex.c:99
99      mutex.c: No such file or directory.
        in mutex.c
(gdb) backtrace full
#0  __pthread_mutex_lock (mutex=0x10) at mutex.c:99
        mutex = (pthread_mutex_t *) 0x10
        self = 0x0
#1  0x403d8f43 in PyThread_acquire_lock (lock=0x0, waitflag=1) at
        lock = 0x0
        success = 1078103364
        status = 0
        error = 0
#2  0x403bbb4e in PyEval_AcquireLock () at Python/ceval.c:267
No locals.
#3  0x4037c1b3 in directive_PythonImport (cmd=0xbffffc30,
mconfig=0x80e0958, module=0x80e0e28 "globals") at mod_python.c:1220
        cmd = (cmd_parms *) 0x0
        mconfig = (void *) 0x0
        module = 0x0
#4  0x08067349 in invoke_cmd (cmd=0x4040d154, parms=0xbffffc30,
mconfig=0x80e0958, args=0x80d594f "") at config.c:800
        w = 0x80e0e28 "globals"
        w2 = 0xbffffc30 ""
        w3 = 0x4040d154 "\231n?@\234Á7@"
        errmsg = 0x0
#5  0x08067a53 in ap_walk_config_sub (current=0x80d5928,
parms=0xbffffc30, section_vector=0x80e05f0) at config.c:1082
        dir_config = (void *) 0x4040d154
        retval = 0x4040d154 "\231n?@\234Á7@"
        cmd = (command_rec *) 0x4040d154
        current = (ap_directive_t *) 0x80d5928
        parms = (cmd_parms *) 0xbffffc30
        mod = (module *) 0x4040d2e0
#6  0x08067ad1 in ap_walk_config (current=0x80d5850, parms=0xbffffc30,
section_vector=0x80e05f0) at config.c:1121
        errmsg = 0x0
        current = (ap_directive_t *) 0x80d5928
        parms = (cmd_parms *) 0xbffffc30
        oldconfig = (ap_conf_vector_t *) 0x80b3488
#7  0x08074beb in dirsection (cmd=0xbffffc30, mconfig=0x80b3598,
arg=0x80e0792 "") at core.c:1625
        cmd = (cmd_parms *) 0xbffffc30
        errmsg = 0x0
        endp = 0x80e07e0 ""
        old_overrides = 150
        old_path = 0x0
        conf = (core_dir_config *) 0x80e07e0
        new_dir_conf = (ap_conf_vector_t *) 0x80e05f0
        r = (regex_t *) 0x0
        thiscmd = (command_rec *) 0x8088bec
        err = 0x0
#8  0x08066e6e in invoke_cmd (cmd=0x8088bec, parms=0xbffffc30,
mconfig=0x80b3598, args=0x80d5828 "/var/www/publisher>") at config.c:713
        w = 0x80d5808 "øW\r\b(X\r\b"
        w2 = 0xbffffc30 ""
        w3 = 0x4040d154 "\231n?@\234Á7@"
        errmsg = 0x0
#9  0x08067a53 in ap_walk_config_sub (current=0x80d5808,
parms=0xbffffc30, section_vector=0x80b3488) at config.c:1082
        dir_config = (void *) 0x4040d154
        retval = 0x4040d154 "\231n?@\234Á7@"
        cmd = (command_rec *) 0x8088bec
        current = (ap_directive_t *) 0x80d5808
        parms = (cmd_parms *) 0xbffffc30
        mod = (module *) 0x808ed60
#10 0x08067ad1 in ap_walk_config (current=0x80b49d0, parms=0xbffffc30,
section_vector=0x80b3488) at config.c:1121
        errmsg = 0x0
        current = (ap_directive_t *) 0x80d5808
        parms = (cmd_parms *) 0xbffffc30
        oldconfig = (ap_conf_vector_t *) 0x0
#11 0x080684e0 in ap_process_config_tree (s=0x80b2ec8,
conftree=0x80b49d0, p=0x8098d10, ptemp=0x80c4dc0) at config.c:1583
        s = (server_rec *) 0x4040d154
        p = (apr_pool_t *) 0x8098d10
        ptemp = (apr_pool_t *) 0x0
        errmsg = 0x808defc ""
        parms = {info = 0x0, override = 95, limited = -1,
limited_xmethods = 0x0, xlimited = 0x0, config_file = 0x0, directive =
0x80d5928, pool = 0x8098d10, 
  temp_pool = 0x80c4dc0, server = 0x80b2ec8, path = 0x80e07c8
"/var/www/publisher/", cmd = 0x4040d154, context = 0x80e05f0,
err_directive = 0x0}
#12 0x0806a811 in main (argc=2, argv=0xbffffd44) at main.c:564
        c = 116 't'
        configtestonly = 1
        confname = 0x8086518 "conf/httpd.conf"
        def_server_root = 0x8086528 "/usr/apache2"
        temp_error_log = 0x0
        process = (process_rec *) 0x8096d88
        server_conf = (server_rec *) 0x80b2ec8
        pglobal = (apr_pool_t *) 0x8096d08
        pconf = (apr_pool_t *) 0x8098d10
        plog = (apr_pool_t *) 0x80c2db8
        ptemp = (apr_pool_t *) 0x80c4dc0
        pcommands = (apr_pool_t *) 0x80c0db0
        opt = (apr_getopt_t *) 0x80c0e48
        rv = 0
        mod = (module **) 0x80b2ec8
        optarg = 0x0
        signal_server = (apr_OFN_ap_signal_server_t *) 0x4040d154
#13 0x40189571 in __libc_start_main (main=0x806a370 <main>, argc=2,
ubp_av=0xbffffd44, init=0x805b824 <_init>, fini=0x80812e0 <_fini>,
rtld_fini=0x4000aa24 <_dl_fini>, 
    stack_end=0xbffffd3c) at ../sysdeps/generic/libc-start.c:129
        ubp_av = (char **) 0xbffffd44
        fini = (void (*)()) 0x400154ac <_dl_debug_mask>
        rtld_fini = (void (*)()) 0x80e0e28
        ubp_ev = (char **) 0x0

Robin Munn
rmunn at pobox.com

More information about the Mod_python mailing list