Skip to content

Commit

Permalink
Protecting sys.path with a thread lock for #612.
Browse files Browse the repository at this point in the history
  • Loading branch information
Pontus Stenetorp committed Jan 6, 2012
1 parent fb03e67 commit 37b1e0a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 32 deletions.
4 changes: 4 additions & 0 deletions ajax.fcgi
Expand Up @@ -10,6 +10,10 @@ Depends on flup:
http://pypi.python.org/pypi/flup/
Or:
sudo apt-get install python-flup
Author: Pontus Stenetorp <pontus is s u-tokyo ac jp>
Version: 2011-09-14
'''
Expand Down
73 changes: 41 additions & 32 deletions server/src/server.py
Expand Up @@ -20,6 +20,7 @@
from os.path import join as path_join
from sys import version_info, stderr
from time import time
from thread import allocate_lock

### Constants
# This handling of version_info is strictly for backwards compability
Expand All @@ -29,6 +30,7 @@
JSON_HDR = ('Content-Type', 'application/json')
CONF_FNAME = 'config.py'
CONF_TEMPLATE_FNAME = 'config_template.py'
CONFIG_CHECK_LOCK = allocate_lock()
###


Expand Down Expand Up @@ -85,39 +87,43 @@ def _config_check():
# Reset the path to force config.py to be in the root (could be hacked
# using __init__.py, but we can be monkey-patched anyway)
orig_path = deepcopy(path)
# Can't you empty in O(1) instead of O(N)?
while path:
path.pop()
path.append(path_join(abspath(dirname(__file__)), '../..'))
# Check if we have a config, otherwise whine
try:
import config
del config
except ImportError, e:
path.extend(orig_path)
# "Prettiest" way to check specific failure
if e.message == 'No module named config':
Messager.error(_miss_config_msg(), duration=-1)
else:
Messager.error(_get_stack_trace(), duration=-1)
raise ConfigurationError
# Try importing the config entries we need
try:
from config import DEBUG
except ImportError:
path.extend(orig_path)
Messager.error(_miss_var_msg('DEBUG'), duration=-1)
raise ConfigurationError

try:
from config import ADMIN_CONTACT_EMAIL
except ImportError:
# Can't you empty in O(1) instead of O(N)?
while path:
path.pop()
path.append(path_join(abspath(dirname(__file__)), '../..'))
# Check if we have a config, otherwise whine
try:
import config
del config
except ImportError, e:
path.extend(orig_path)
# "Prettiest" way to check specific failure
if e.message == 'No module named config':
Messager.error(_miss_config_msg(), duration=-1)
else:
Messager.error(_get_stack_trace(), duration=-1)
raise ConfigurationError
# Try importing the config entries we need
try:
from config import DEBUG
except ImportError:
path.extend(orig_path)
Messager.error(_miss_var_msg('DEBUG'), duration=-1)
raise ConfigurationError
try:
from config import ADMIN_CONTACT_EMAIL
except ImportError:
path.extend(orig_path)
Messager.error(_miss_var_msg('ADMIN_CONTACT_EMAIL'), duration=-1)
raise ConfigurationError
finally:
# Remove our entry to the path
while path:
path.pop()
# Then restore it
path.extend(orig_path)
Messager.error(_miss_var_msg('ADMIN_CONTACT_EMAIL'), duration=-1)
raise ConfigurationError
# Remove our entry to the path
path.pop()
# Then restore it
path.extend(orig_path)

# Convert internal log level to `logging` log level
def _convert_log_level(log_level):
Expand Down Expand Up @@ -268,7 +274,10 @@ def serve(params, client_ip, client_hostname, cookie_data):
from message import Messager

try:
_config_check()
# We need to lock here since flup uses threads for each request and
# can thus manipulate each other's global variables
with CONFIG_CHECK_LOCK:
_config_check()
except ConfigurationError:
return cookie_hdrs, ((JSON_HDR, ), dumps(Messager.output_json({})))
# We can now safely read the config
Expand Down

0 comments on commit 37b1e0a

Please sign in to comment.