Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit59ef0f0

Browse files
committed
JsonPickler
1 parent5d3a484 commit59ef0f0

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
lines changed

‎backend/main/views.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,7 @@ def run_code(self, code, source):
121121

122122
forcallinbirdseye_objects["calls"]:
123123
call["function_id"]=function_ids[call["function_id"]]
124-
ifisinstance(call["start_time"],str):
125-
call["start_time"]=datetime.fromisoformat(call["start_time"])
124+
call["start_time"]=datetime.fromisoformat(call["start_time"])
126125
call=eye.db.Call(**call)
127126
session.add(call)
128127
# TODO get correct call from top level

‎backend/main/workers/master.py‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
importatexit
2-
importmultiprocessing
32
importqueue
43
fromcollectionsimportdefaultdict
54
fromfunctoolsimportlru_cache
6-
frommultiprocessing.contextimportProcess
5+
frommultiprocessingimportQueue,Process
76
fromthreadingimportThread
87

98
frommainimportsimple_settings
@@ -15,10 +14,10 @@
1514

1615

1716
classUserProcess:
18-
def__init__(self,manager):
19-
self.task_queue=manager.Queue()
20-
self.input_queue=manager.Queue()
21-
self.result_queue=manager.Queue()
17+
def__init__(self):
18+
self.task_queue=Queue()
19+
self.input_queue=Queue()
20+
self.result_queue=Queue()
2221
self.awaiting_input=False
2322
self.process=None
2423
self.start_process()
@@ -85,8 +84,7 @@ def _await_result(self):
8584

8685
defmaster_consumer_loop(comms:AbstractCommunications):
8786
comms=comms.make_master_side_communications()
88-
manager=multiprocessing.Manager()
89-
user_processes=defaultdict(lambda:UserProcess(manager))
87+
user_processes=defaultdict(UserProcess)
9088

9189
whileTrue:
9290
entry=comms.recv_entry()

‎backend/main/workers/utils.py‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
importsys
22
importtraceback
3+
importmultiprocessing.queues
4+
importjson
35

6+
fromlittleutilsimportDecentJSONEncoder
47
fromsentry_sdkimportcapture_exception
58

69

@@ -36,6 +39,8 @@ def string(self):
3639

3740
output_buffer=OutputBuffer()
3841

42+
json_encoder=DecentJSONEncoder()
43+
3944

4045
defmake_result(
4146
passed=False,
@@ -52,7 +57,7 @@ def make_result(
5257
ifoutput_partsisNone:
5358
output_parts=output_buffer.pop()
5459

55-
returndict(
60+
result=dict(
5661
passed=passed,
5762
message=message,
5863
awaiting_input=awaiting_input,
@@ -61,6 +66,10 @@ def make_result(
6166
birdseye_objects=birdseye_objects,
6267
error=error,
6368
)
69+
# Check that JSON encoding works here
70+
# because failures in the queue pickling are silent
71+
json_pickler.dumps(result)
72+
returnresult
6473

6574

6675
definternal_error_result(sentry_offline=False):
@@ -87,3 +96,16 @@ def internal_error_result(sentry_offline=False):
8796
output_parts=[dict(color="red",text=output)],
8897
error=dict(traceback=tb,sentry_event=sentry_event),
8998
)
99+
100+
101+
# Queues don't communicate in pickle so that the worker
102+
# can't put something malicious for the master to unpickle
103+
classJsonPickler:
104+
defloads(self,b):
105+
returnjson.loads(b.decode("utf8"))
106+
107+
defdumps(self,x):
108+
returnjson_encoder.encode(x).encode("utf8")
109+
110+
111+
multiprocessing.queues._ForkingPickler=json_pickler=JsonPickler()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp