# Unicorn 0.96.1 - leak fix for Rainbows!/Zbatery

**URL:** <https://www.ruby-forum.com/t/unicorn-0-96-1-leak-fix-for-rainbows-zbatery/183410>\
**Category:** Ruby\
**Created:** [February 13, 2010, 11:34am UTC](https://www.ruby-forum.com/t/unicorn-0-96-1-leak-fix-for-rainbows-zbatery/183410 "2010-02-13T11:34:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ericw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/97f17d/32.png) [@ericw](https://www.ruby-forum.com/u/ericw)\
**Post date:** [February 13, 2010, 11:34am UTC](https://www.ruby-forum.com/t/unicorn-0-96-1-leak-fix-for-rainbows-zbatery/183410/1 "2010-02-13T11:34:56Z")

</div>

First off, this memory leak doesn’t affect Unicorn itself at all  
(but it doesn’t hurt, either).

Unicorn itself always allocates the HttpParser once and always reuses it  
in every sequential request.

This leak only affects applications that repeatedly allocate a new HTTP  
parser. Thus this bug affects _all_ deployments of Rainbows! and  
Zbatery. These servers allocate a new parser for every client  
connection to serve clients concurrently, but due to a bug in Unicorn,  
never allows the Ruby GC to properly free the memory allocated.

Here’s what happened:

I misread the Data\_Make\_Struct()/Data\_Wrap\_Struct()  
documentation and ended up passing NULL as the “free” argument  
instead of -1, causing the memory to never be freed.

From README.EXT in the MRI source which I misread:

> The free argument is the function to free the pointer  
> allocation. If this is -1, the pointer will be just freed.  
> The functions mark and free will be called from garbage  
> collector.

Yes, I suck at reading and can’t write code properly as a result.

- [http://unicorn.bogomips.org/](http://unicorn.bogomips.org/)
- removed\_email\_address@domain.invalid
- git://git.bogomips.org/unicorn.git

---

<div class="post-metadata">

**Author:** ![ericw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/97f17d/32.png) [@ericw](https://www.ruby-forum.com/u/ericw)\
**Post date:** [February 13, 2010, 11:39am UTC](https://www.ruby-forum.com/t/unicorn-0-96-1-leak-fix-for-rainbows-zbatery/183410/2 "2010-02-13T11:39:02Z")

</div>

This release depends on Unicorn 0.96.1 for an updated  
Unicorn::HttpParser to avoid leaking memory.

The HttpParser in Unicorn \<= 0.96.0 did not setup the parser  
object properly to be freed by the garbage collector.

While this bug did not affect Unicorn itself, Rainbows!  
allocates a new Unicorn::HttpParser object for every new client  
connection and Unicorn did not properly setup the parser object  
to be freed by the Ruby garbage collector.

There are also minor cosmetic cleanups and fixes:

Eric W. (10):  
http\_response: disallow blank, multi-value headers  
Fix “rainbows -h” and “rainbows -v”  
Update docs + tests to reflect Rev 0.3.2 release  
local.mk.sample: bump Rack dependency  
Merge branch ‘rack-1.1’  
add Cramp integration tests  
Rakefile: autoload Gem  
t/bin/\*: encoding should be the first line after shebang  
gemspec: bump dependency on Unicorn to avoid leak  
Rainbows! 0.90.2

- [http://rainbows.rubyforge.org/](http://rainbows.rubyforge.org/)
- removed\_email\_address@domain.invalid
- git://git.bogomips.org/rainbows.git

---

<div class="post-metadata">

**Author:** ![ericw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/97f17d/32.png) [@ericw](https://www.ruby-forum.com/u/ericw)\
**Post date:** [February 13, 2010, 11:39am UTC](https://www.ruby-forum.com/t/unicorn-0-96-1-leak-fix-for-rainbows-zbatery/183410/3 "2010-02-13T11:39:37Z")

</div>

Unicorn had a memory leak that didn’t affect Unicorn, but only  
Rainbows!, so we bumped the dependency on Rainbows! which in turn  
bumped the dependency on Unicorn…

Also some minor documentation updates.

- [http://zbatery.bogomip.org/](http://zbatery.bogomip.org/)
- removed\_email\_address@domain.invalid
- git://git.bogomips.org/zbatery.git

---

<div class="post-metadata">

**Author:** ![ericw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/97f17d/32.png) [@ericw](https://www.ruby-forum.com/u/ericw)\
**Post date:** [February 13, 2010, 1:16pm UTC](https://www.ruby-forum.com/t/unicorn-0-96-1-leak-fix-for-rainbows-zbatery/183410/4 "2010-02-13T13:16:09Z")

</div>

Almost forgot about this one, I have a short attention span  
_and_ no ability to comprehend what I read!

Changes:

This fixes a misuse of the Ruby API leading to memory leaks in  
cases where message queues are continually opened and closed  
throughout the lifetime of the application.

Fortunately applications have little reason to repeatedly open  
and close message queue descriptors: they are  
multi-thread/multi-process-safe in every way imaginable and also  
capable of non-blocking operation.

POSIX message queues allow local processes to exchange data in the form  
of messages. This API is distinct from that provided by System V  
message queues, but provides similar functionality.

- [posix\_mq - POSIX message queues for Ruby](http://bogomips.org/ruby_posix_mq/)
- removed\_email\_address@domain.invalid
- git://git.bogomips.org/ruby\_posix\_mq.git
