Skip to content

Commit

Permalink
Remove unserializable data from metadata in Log records (#2469)
Browse files Browse the repository at this point in the history
If unserializable data, often found in the `exc_info` or `args` keys, is
not removed from the metadata of a log record, a database exception will
be thrown.
  • Loading branch information
sphuber committed Feb 9, 2019
1 parent 3f0365d commit b85ac3e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
26 changes: 25 additions & 1 deletion aiida/backends/tests/orm/logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,31 @@ def test_create_log_message(self):
self.assertEqual(entry.message, self.log_record['message'])
self.assertEqual(entry.metadata, self.log_record['metadata'])

def test_create_log_unserializable_metadata(self):
"""Test that unserializable data will be removed before reaching the database causing an error."""
import functools

def unbound_method(argument):
return argument

partial = functools.partial(unbound_method, 'argument')

node = CalculationNode().store()

# An unbound method in the `args` of the metadata
node.logger.error('problem occurred in method %s', unbound_method)

# A partial in the `args` of the metadata
node.logger.error('problem occurred in partial %s', partial)

# An exception which will include an `exc_info` object
try:
raise ValueError
except ValueError:
node.logger.exception('caught an exception')

self.assertEqual(len(Log.objects.all()), 3)

def test_log_delete_single(self):
"""Test that a single log entry can be deleted through the collection."""
entry, _ = self.create_log()
Expand Down Expand Up @@ -186,7 +211,6 @@ def test_db_log_handler(self):
# Launching a second log message ensuring that both messages are correctly stored
message2 = message + " - Second message"
node.logger.critical(message2)
# logs = Log.objects.find()

order_by = [OrderSpecifier('time', ASCENDING)]
logs = Log.objects.find(order_by=order_by)
Expand Down
6 changes: 4 additions & 2 deletions aiida/orm/logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ def create_entry_from_record(record):

metadata = dict(record.__dict__)

# Get rid of the exc info because this is usually not serializable
metadata['exc_info'] = None
# Stringify the content of `exc_info` and `args` if they exist in the metadata to ensure serializability
for key in ['exc_info', 'args']:
if key in metadata:
metadata[key] = str(metadata[key])

return Log(
time=timezone.make_aware(datetime.fromtimestamp(record.created)),
Expand Down

0 comments on commit b85ac3e

Please sign in to comment.