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

Possible race condition in the Maildown::MarkdownEngine.default_html_block method #40

Closed
yuki24 opened this issue Mar 27, 2018 · 5 comments

Comments

@yuki24
Copy link

yuki24 commented Mar 27, 2018

I looked into what has caused the bug ruby/did_you_mean#102. One thing I noticed is that in the maildown gem, the kramdown gem is lazily loaded on the fly, which adds a handful of constants dynamically. In an extreme condition, this may cause a race condition. Consider the following scenario where a Sidekiq instance runs two threads (workders) that attempt to run an email delivery job:

Thread 1 Thread 2
calls MarkdownEngine.default_html_block -
- calls MarkdownEngine.default_html_block
attempts to require 'kramdown' for the first time -
evaluates the line that adds a Kramdown constant but doesn't reach the line 74 that adds Kramdown::Document -
- evaluates unless defined? Kramdown (now evaluates to unless true)
- attempts to call html_block.call on the proc that has a reference to the Kramdown::Document that does not exist yet

In this scenario, the thread 2 would raise a NameError because the Kramdown constant is loaded but the Kramdown::Document is not. Also, it would only occur once after a cold reboot since the constant would be available once it's loaded by the thread 1.

I wasn't sure what fixes you'd prefer, so just wanted to bring this up. Thanks!

@schneems
Copy link
Member

Thanks for the note. The reason that it's a lazy load is that people can install their own markdown rendering engines if they want (though honestly I have no proof that anyone is doing that.

I think the fix would be requiring the gem at boot time, but rescuing in-case it does not exist. The only other thing I could think of is to use a mutex to block multiple clients from trying to require at the same time, but that increases a tiny bit of runtime overhead that I don't think is needed.

schneems added a commit that referenced this issue Mar 27, 2018
There is a race condition described in #40 where two threads can require the same library.
@schneems schneems mentioned this issue Mar 27, 2018
@schneems
Copy link
Member

Do you know if Autoload fixes this issue, or if I need to mutex it?

@yuki24
Copy link
Author

yuki24 commented Mar 27, 2018

I was late by a hair, but you may have to mutex it. There is a thread-safety issue in Autoload https://bugs.ruby-lang.org/issues/12688 and the example in the issue happens to reproduce the exact same issue you saw a few days ago. Not sure that was fixed.

cc @ko1

@schneems
Copy link
Member

Looks like it is threadsafe https://twitter.com/schneems/status/978705647301746688

@bf4
Copy link

bf4 commented Mar 28, 2018

It's supposed to have been thread safe since 2.0 headius/thread_safe#15 (comment)

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

No branches or pull requests

3 participants