Forums

Is there a way to programmatically reload the web app ?

My demo of the Python regex tester (http://ksamuel.pythonanywhere.com/) is vulnerable to bad users entries.

This is just a demo, beginers use it to learn regex, and sometimes they just write a bad one, with so many backreferences that it kills the process.

There is no good way to prevent that: - you can't find all edge cases with regex. - regex aquire the Python GIL, so you cannot use threads, signals, or else.

I looking to either:

  • reload the web app when the user hit a 500
  • reload the web app every 20 minutes

Can you help me with that ?

Hm. could you spawn a separate process for running the user's regexes?

eg, given test_string and users_regex, something like:

temp_script = """
import re
test_string = '''%s'''
users_regex = r'%s'
try:
    print re.search(users_regex, test_string)
except:
    print "Error in regex"
""" % (test_string, users_regex)

tf = NamedTemporaryFile()
tf.write(temp_script)

process = subprocess.call(
    ['python', tf.name], 
    stdout=subprocess.PIPE, stderr=subprocess.PIPE
)

solution = process.stdout.read()

```

... something like that? With some tweaking of the various try/catches maybe, or spawning some threads to timeout after a certain time?

That's a good idea. I'm going to try that.