Ruby’s puts is not atomic

Paul Kuruvilla
Apr 2, 2017 · 2 min read

While improving a build script by parallelising actions, I realised that Ruby’s puts is not atomic.

Effects of the race condition in action

Looks like puts is calling the underlying write function twice - Once to actually print the variable given to it, and once again to print the newline character. This leads to a race condition in puts, as observed above.

I confirmed this by taking a look at the source code for puts in Rubinius and MRI.

Image for post
Image for post
Image for post
Image for post
Annotated source code for puts in MRI, Rubinius

There is an open issue to fix this in MRI to fix this by using writev instead of writehttps://bugs.ruby-lang.org/issues/9420

I worked around this by creating a safe_puts function in our code, it looked like this:

Image for post
Image for post
By supplying ‘\n’ explicitly at the end, the whole string gets printed at once

This seems to be a problem with Python too, according to this StackOverflow post.

Hacker Noon is how hackers start their afternoons. We’re a part of the @AMI family. We are now accepting submissions and happy to discuss advertising & sponsorship opportunities.

If you enjoyed this story, we recommend reading our latest tech stories and trending tech stories. Until next time, don’t take the realities of the world for granted!

Image for post
Image for post

HackerNoon.com

#BlackLivesMatter

Sign up for Get Better Tech Emails via HackerNoon.com

By HackerNoon.com

how hackers start their afternoons. the real shit is on hackernoon.com. Take a look

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Paul Kuruvilla

Written by

Practical and fact-minded individual, whose reliability cannot be doubted.

HackerNoon.com

Elijah McClain, George Floyd, Eric Garner, Breonna Taylor, Ahmaud Arbery, Michael Brown, Oscar Grant, Atatiana Jefferson, Tamir Rice, Bettie Jones, Botham Jean

Paul Kuruvilla

Written by

Practical and fact-minded individual, whose reliability cannot be doubted.

HackerNoon.com

Elijah McClain, George Floyd, Eric Garner, Breonna Taylor, Ahmaud Arbery, Michael Brown, Oscar Grant, Atatiana Jefferson, Tamir Rice, Bettie Jones, Botham Jean

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store