Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix threafing bug with send buffer and threaded workers #335

Closed
wants to merge 3 commits into from

Conversation

beniwohli
Copy link
Contributor

@beniwohli beniwohli commented Nov 15, 2018

When using threaded workers (e.g. Django runserver in Django 2.0+),
writes to the send buffer need to be guarded by a threading lock.
Otherwise we end up with garbled content in the buffer, which then later
on leads to parsing errors in the APM Server.

fixes #334

When using threaded workers (e.g. Django runserver in Django 2.0+),
writes to the send buffer need to be guarded by a threading lock.
Otherwise we end up with garbled content in the buffer, which then later
on leads to parsing errors in the APM Server.

fixes elastic#334
self._last_flush = timeit.default_timer()
self._flush_timer = None

def queue(self, event_type, data, flush=False):
self._queue(self.queued_data, {event_type: data})
with self._queue_lock:
self.queued_data.write((self._json_serializer({event_type: data}) + "\n").encode("utf-8"))
since_last_flush = timeit.default_timer() - self._last_flush
queue_size = self.queued_data_size
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.queued_data_size calls self.queued_data so calls to self.queued_data_size must also be protected by the lock, or you lock inside self.queued_data

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch. I'll remove the call to self.queued_data in queued_data_size and instead check for existence of self._queued_data directly and return 0 if it doesn't exist. Then we don't need a lock as it is read only. Makes sense?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah that would probably work. It seems like queue_size here could end up being the size of a queue that was just flushed and we would then flush again, but that might not be an issue

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree, this shouldn't really matter

Copy link
Contributor

@simitt simitt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

this makes the use of a lock for this method unnecessary
@ciarancourtney
Copy link

Just an FYI on commit msg, Django runserver is threaded since 1.4

@beniwohli
Copy link
Contributor Author

@ciarancourtney oh, you're right. Weirdly enough, I can't replicate the issue in Django 1.11 with runserver

beniwohli added a commit that referenced this pull request Nov 16, 2018
When using threaded workers (e.g. Django runserver),
writes to the send buffer need to be guarded by a threading lock.
Otherwise we end up with garbled content in the buffer, which then later
on leads to parsing errors in the APM Server.

fixes #334
@beniwohli
Copy link
Contributor Author

merged in 4a02ee5

@beniwohli beniwohli closed this Nov 19, 2018
@beniwohli beniwohli deleted the fix-334 branch November 19, 2018 08:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[4.0.0] UnicodeDecodeError: 'utf-8' codec can't decode byte 0x96 in position 91: invalid start byte
4 participants