N. Simon N. Simon - 1 month ago 13
Python Question

flask running in mod_wsgi cannot write to /tmp

Apache v2.4.12-2

Mod_wsgi v4.4.8-1

Python v3.4.2

python-flask v0.10.1-5

Arch linux - kernel 3.12.36

I'm using mod_wsgi and flask to host a server. I am able to reproduce this issue with the following simplified code and generic .wsgi script:

MainServer.py:

import flask

app = flask.Flask(__name__)

@app.before_first_request
def initstuff():
test_file = '/tmp/test'
with open(test_file, 'w') as f:
f.write('test')

@app.route('/', methods=['GET'])
def rootdir():
return 'Hello world'


MainServer.wsgi:

from MainServer import app as application


Expected: a file with the contents 'test' is written in /tmp

Actual outcome: No file is written. No errors reported in log

If I run the same code but instead point to any other directory which my user has permission to write, it creates the file as expected. /tmp is the only directory where I am having this issue.

If I run the above code directly and use flask's built in server (app.run), it can create the file in /tmp as expected without any issues.

I've ensured that the mod_wsgi server is running as the same user as the script with app.run is and that this user is able to write to /tmp.

--edit--

Running httpd directly from the command line does not cause this issue. Starting httpd as a systemd service does

Answer

I ended up simply moving the file I was attempting to create out of /tmp. The original issues appears to be related to Systemd and special permissions for /tmp (although it persists when changing permissions from 1777 to just 777), but I was not able to figure it out the cause or a solution for it.