summaryrefslogtreecommitdiff
path: root/server/python_session.py
blob: 52a54de0025fbce5fbe2099f2400c8e33f8e650d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# CodeQ: an online programming tutor.
# Copyright (C) 2015 UL FRI
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from fcntl import fcntl, F_GETFL, F_SETFL
import io
import multiprocessing
import os
import pickle
import selectors
import signal
import subprocess
import sys
import threading
import time

import server
import server.user_session
from db.models import Problem

__all__ = ['PythonSession']

class PythonSession(server.LanguageSession):
    """Abstracts a Python session.
    Only public methods are available to the outside world due to the use of multiprocessing managers.
    Therefore prefix any private methods with an underscore (_).
    No properties are accessible; use getters and setters instead.
    Values are passed by value instead of by reference (deep copy!).
    """
    def __init__(self, output_cb=None):
        self._access_lock = threading.Lock()
        self._sent_hints = []

        self._notifier, receiver = multiprocessing.Pipe()
        self._interpreter = threading.Thread(target=_interpreter,
                                         kwargs={'control': receiver, 'callback': output_cb})
        self._interpreter.start()

    def run(self, code=None, inputs=None, timeout=1.0):
        # Launch processes.
        futures = []
        for expr, stdin in inputs:
            p = subprocess.Popen(proc_args + ['run'],
                    stdin=subprocess.PIPE,
                    stdout=subprocess.PIPE,
                    stderr=subprocess.DEVNULL)
            data = pickle.dumps((code, expr, stdin))
            p.stdin.write(data)
            futures.append(p)

        # Wait for results.
        results = []
        start = time.monotonic()
        for proc in futures:
            now = time.monotonic()
            real_timeout = max(0, timeout - (now - start))
            try:
                stdout, _ = proc.communicate(timeout=real_timeout)
                result = pickle.loads(stdout)
                results.append(result)
            except subprocess.TimeoutExpired:
                results.append((None, '', '', 'timed out'))
            except EOFError:
                results.append((None, '', '', 'sandbox violation'))
            if proc.poll() is None:
                try:
                    proc_kill(proc, signal.SIGKILL)
                except:
                    pass

        return results

    def exec(self, program):
        self._notifier.send(('exec', program))

    def push(self, stdin):
        self._notifier.send(('push', stdin))

    def stop(self):
        self._notifier.send(('stop', None))

    def destroy(self):
        self._notifier.send(('done', None))

    def __del__(self):
        self.destroy()

    # TODO remove this
    def hint(self, sid, problem_id, program):
        return []

    def test(self, sid, problem_id, program):
        p = Problem.get(id=problem_id)
        language_module = server.problems.load_language(p.language, 'common')
        problem_module = server.problems.load_problem(p.language, p.group, p.identifier, 'common')

        # check if the program is correct
        passed, msgs = problem_module.test(self.run, program)
        if passed:
            session = server.user_session.get_session_by_id(sid)
            session.update_solution(problem_id, solution=program, done=True)
        else:
            hints = []
            if not hints and hasattr(language_module, 'hint'):
                hints = language_module.hint(self.run, program)
            if not hints and hasattr(problem_module, 'hint'):
                hints = problem_module.hint(self.run, program)
            if hints:
                msgs.extend(hints)

        self._instantiate_and_save_hints(language_module, problem_module, msgs)
        return msgs

    # Add hint parameters (such as message index) based on hint class. Append
    # the finalized hints to the list of sent hints.
    def _instantiate_and_save_hints(self, language_mod, problem_mod, hints):
        with self._access_lock:
            for hint in hints:
                for mod in [language_mod, problem_mod]:
                    if hasattr(mod, 'hint_type') and hint['id'] in mod.hint_type:
                        hint_type = mod.hint_type[hint['id']]
                        hint_type.instantiate(hint, self._sent_hints)
            self._sent_hints.extend(hints)

# If the sandbox wrapper exists, use it to switch to user "nobody" and enforce
# additional limits. Unless the daemon is running as root we are not able to
# signal nobody's PIDs, so switch user again for the killing.
_basedir = os.path.split(os.path.dirname(os.path.realpath(__file__)))[0]
_script = os.path.join(_basedir, 'python', 'runner', 'interpreter.py')
_sandbox = os.path.join(_basedir, 'python', 'runner', 'sandbox')
if os.path.exists(_sandbox):
    newuser = 'nobody'  # TODO make this configurable
    proc_args = [_sandbox, newuser, _script]
    proc_kill = lambda proc, sig: subprocess.call([_sandbox, newuser, 'kill', '-{}'.format(sig), str(proc.pid)])
else:
    proc_args = [_script]
    proc_kill = lambda proc, sig: proc.send_signal(sig)

def _interpreter(control, callback):
    done = False
    proc = None

    # Remember how much text and how many newlines we received this second; if
    # it is too much, kill the interpreter.
    # TODO this is a hack to prevent the JS console from becoming unresponsive,
    # it should be fixed there.
    now = 0
    length = newlines = 0

    selector = selectors.DefaultSelector()

    def command(conn):
        nonlocal proc, done
        # Get a control command.
        try:
            cmd, data = conn.recv()
            if cmd == 'exec':
                exec_str = 'exec("""\\\n{}\n""")\n'.format(data.replace('"', '\\"'))
                proc.stdin.write(exec_str.encode('utf-8'))
                proc.stdin.flush()
            elif cmd == 'push':
                proc.stdin.write(data.encode('utf-8'))
                proc.stdin.flush()
            elif cmd == 'stop':
                proc_kill(proc, signal.SIGINT)
            elif cmd == 'done':
                done = True
        except:
            pass

    def communicate(conn):
        nonlocal proc, callback, now, length, newlines
        if time.monotonic() - now > 1.0:
            length = newlines = 0
            now = time.monotonic()

        # Communicate with child process.
        data = proc.stdout.read1(1024)
        if data and length < 100000 and newlines < 1000:
            # NOTE this might fail if read() stops in the middle of utf8 sequence
            text = data.decode('utf-8')
            if text:
                callback(text)
                length += len(text)
                newlines += text.count('\n')
        else:
            selector.unregister(conn)
            if proc.poll() is None:
                # Process has not terminated yet, make sure it does.
                proc_kill(proc, signal.SIGKILL)
            proc = None
            length = newlines = 0
            callback('Interpreter restarted.\n')

    selector.register(control, selectors.EVENT_READ, command)
    while not done:
        # Ensure the interpreter process is running.
        if proc is None:
            proc = subprocess.Popen(proc_args,
                    stdin=subprocess.PIPE,
                    stdout=subprocess.PIPE,
                    stderr=subprocess.STDOUT)
            # Set the non-blocking flag for stdout.
            flags = fcntl(proc.stdout.fileno(), F_GETFL)
            fcntl(proc.stdout.fileno(), F_SETFL, flags | os.O_NONBLOCK)
            selector.register(proc.stdout, selectors.EVENT_READ, communicate)

        events = selector.select()
        for key, mask in events:
            if mask & selectors.EVENT_READ:
                try:
                    key.data(key.fileobj)
                except:
                    pass

    # We are done, kill the child.
    selector.close()
    if proc is not None:
        proc_kill(proc, signal.SIGKILL)

server.language_session_handlers['python'] = lambda user_session, problem_id, language_identifier, group_identifier, problem_identifier: PythonSession(lambda text: user_session.send({'event': 'terminal_output', 'text': text}))