Skip to content

Commit

Permalink
Fix error raised on NULL return
Browse files Browse the repository at this point in the history
Empty values, such as those returned from functions run for their side
effects, like simple print statements that write to files, are already
handled correctly internally. The are stored as 0 bytes in voidstar
format. However, in the final checking in the nexus I was raising an
error if no content was returned, this led to an incorrect failing state
after successfully running the code. That is now fixed.

However, in the course of testing and fixing this code, I was reminded
of an old problem with morloc: how to handle STDERR and STDOUT. In the
past, I passed data between pools and the nexus through the STDOUT
channel. In order for this to work, I had to ensure that the channel was
not corrupted by any STDOUT written by the wrapped functions. So I
captured any STDOUT from these runs and stored it in the MorlocMonad for
possible later use. Same with STDERR. This behavior, though, hampers
debugging and is no longer necessary since I pass data over sockets.
  • Loading branch information
arendsee committed Jan 4, 2025
1 parent 4b03c99 commit bf4beac
Showing 1 changed file with 37 additions and 29 deletions.
66 changes: 37 additions & 29 deletions data/nexus.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,44 +448,52 @@ def print_return(data, schema_str):
clean_exit(1, f"Implementation bug: expected data packet: {str(data)}")

content = None
isgood = True
error = ""

if cmd_source == PACKET_SOURCE_MESG:
content = data[data_start: ]
elif cmd_source == PACKET_SOURCE_FILE:
filename = data[data_start:].decode()
with open(filename, 'rb') as file:
content = file.read()
os.unlink(filename) # Delete the temporary file
elif cmd_source == PACKET_SOURCE_RPTR:
relptr = _unpack("Q", data[data_start:])[0]
_log(f"Made relative pointer {relptr!s} for return")
content = mp.from_shm(relptr, schema_str)

if content == None:
error = "Unexpected source"
isgood = False
try:
if cmd_source == PACKET_SOURCE_MESG:
content = data[data_start: ]
elif cmd_source == PACKET_SOURCE_FILE:
filename = data[data_start:].decode()
with open(filename, 'rb') as file:
content = file.read()
os.unlink(filename) # Delete the temporary file
elif cmd_source == PACKET_SOURCE_RPTR:
relptr = _unpack("Q", data[data_start:])[0]
_log(f"Made relative pointer {relptr!s} for return")
content = mp.from_shm(relptr, schema_str)
except Exception as e:
errmsg = f"Failed to open return data packet: {e!s}"
clean_exit(1, errmsg)

if content == None and schema_str == "z":
# if we expected a NULL result and got one, do nothing
pass
elif content == None and schema_str != "z":
errmsg = "Return data was expected but none was found"
clean_exit(1, errmsg)
elif cmd_format == PACKET_FORMAT_JSON:
print(content, file=outfile)
isgood = True

elif cmd_format == PACKET_FORMAT_MSGPACK:
try:
json.dump(mp.mesgpack_to_py(content, schema_str), fp = sys.stdout, separators = JSON_SEPARATORS)
print() # newline
except Exception as e:
_log(trace(f"Failed to read output MessagePack format with error {str(e)}:\n hex = {hex(content)}\n chr = {str(content)}"))
print() # newline
isgood = True
errmsg = f"Failed to read output MessagePack format with error:\n{e!s}"
_log(trace(errmsg))
clean_exit(1, errmsg)
elif cmd_format == PACKET_FORMAT_VOIDSTAR:
json.dump(content, fp = sys.stdout, separators = JSON_SEPARATORS)
print() # newline

# Exit with the proper error code
if isgood:
clean_exit(exit_code)
try:
json.dump(content, fp = sys.stdout, separators = JSON_SEPARATORS)
print() # newline
except Exception as e:
errmsg = f"Failed to read output voidstar format with error:\n{e!s}"
_log(trace(errmsg))
clean_exit(1, errmsg)
else:
clean_exit(1, error)
errmsg = "Unexpected source"
clean_exit(1, errmsg)

clean_exit(exit_code)


def _make_header(
Expand Down

0 comments on commit bf4beac

Please sign in to comment.