# Multi-threaded DBI causing segfault

**URL:** <https://www.ruby-forum.com/t/multi-threaded-dbi-causing-segfault/181953>\
**Category:** Ruby\
**Created:** [January 20, 2010, 6:11pm UTC](https://www.ruby-forum.com/t/multi-threaded-dbi-causing-segfault/181953 "2010-01-20T18:11:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xavier\_NoSSSSlle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/58f4c7/32.png) [@Xavier\_NoSSSSlle](https://www.ruby-forum.com/u/Xavier_NoSSSSlle)\
**Post date:** [January 20, 2010, 6:11pm UTC](https://www.ruby-forum.com/t/multi-threaded-dbi-causing-segfault/181953/1 "2010-01-20T18:11:32Z")

</div>

Hello,  
I’m new to this mailing list, so do not hesitate to tell me if  
everything’s ok with my post.

I’m using DBI to connect to a MySQL database. I tried to reduce my  
code as much as possible, knowing that:

- constants are valid (not ‘\*\*\*\*’) and lead to valid databases, valid  
tables and the select returns a valid result
- removing the finish() does not solve the problem
- limiting the result size (eg: ‘LIMIT 0, 30’) does not solve the  
problem
- NB\_THREADS is set to 50 to make it crash quickly (but, starting  
from 2, any value makes it crash)
- as you can see below, I’m using Ruby 1.8.7 on Ubuntu 9.10 64 bits
- the file and line number in which the segmentation fault is  
reported is variable (sometimes, it happens my own files)

The error output is as follow:  
/usr/lib/ruby/1.8/dbd/mysql/statement.rb:36: [BUG] Segmentation fault  
ruby 1.8.7 (2009-06-12 patchlevel 174) [x86\_64-linux]

Question: is it a real bug or am I doing something wrong ?  
If I’m wrong, where ?  
If I’m not, is there a workaround to thread the execution of code  
using DBI without a segfault ?

Thanks for any hint about my problem 🙂

=================  
#! /usr/bin/env ruby

require ‘rubygems’  
require ‘dbi’

PRODUCTS\_DB = ‘\*\*\*\*\*’

PRODUCTS\_DB\_HOST = ‘_**’  
PRODUCTS\_DB\_USER = '**_’  
PRODUCTS\_DB\_PASS = ‘\*\*\*\*’

def getDbh()  
return DBI.connect(“DBI:Mysql:#{PRODUCTS\_DB}:”+  
“#{PRODUCTS\_DB\_HOST}”, PRODUCTS\_DB\_USER,  
PRODUCTS\_DB\_PASS)  
end

NB\_THREADS = 50  
threads = Array.new(NB\_THREADS)

NB\_THREADS.times() do |i|  
puts i  
threads[i] = Thread.new() do  
dbh = getDbh()

```
while true
  hdl = dbh.execute('SELECT * FROM ****** LIMIT 0, 30') do |dummy|
    dummy[0]
  end
  hdl.finish()
  puts 'There'
end

```

end  
end

# threads.each() do |t| t.join() end

---

<div class="post-metadata">

**Author:** ![Xavier\_NoSSSSlle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/58f4c7/32.png) [@Xavier\_NoSSSSlle](https://www.ruby-forum.com/u/Xavier_NoSSSSlle)\
**Post date:** [January 20, 2010, 7:04pm UTC](https://www.ruby-forum.com/t/multi-threaded-dbi-causing-segfault/181953/2 "2010-01-20T19:04:48Z")

</div>

> /usr/lib/ruby/1.8/dbd/mysql/statement.rb:36: [BUG] Segmentation fault  
> ruby 1.8.7 (2009-06-12 patchlevel 174) [x86\_64-linux]
> 
> Question: is it a real bug or am I doing something wrong ?  
> If I’m wrong, where ?  
> If I’m not, is there a workaround to thread the execution of code  
> using DBI without a segfault ?

Maybe use the mysql gem?

Also, you could run it in gdb to try to debug the cause.

[http://wiki.github.com/rdp/ruby\_tutorials\_core/building-mri#debug\_build](http://wiki.github.com/rdp/ruby_tutorials_core/building-mri#debug_build)

---

<div class="post-metadata">

**Author:** ![Xavier\_NoSSSSlle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/58f4c7/32.png) [@Xavier\_NoSSSSlle](https://www.ruby-forum.com/u/Xavier_NoSSSSlle)\
**Post date:** [January 21, 2010, 11:22am UTC](https://www.ruby-forum.com/t/multi-threaded-dbi-causing-segfault/181953/3 "2010-01-21T11:22:50Z")

</div>

On Jan 21, 2010, at 11:00 AM, Xavier Noëlle wrote:

> Program received signal SIGSEGV, Segmentation fault.  
> 0x00007ffff7b77517 in st\_foreach () from /usr/lib/libruby1.8.so.1.8

Be aware that the Debian (and thus Ubuntu) maintainers do mangle with  
the  
standard Ruby distribution in non-trivial ways, so you might be their  
fault.

Although I cannot imagine why such a thing would happen, because their  
patches  
usually concern packaging issues.

But it shouldn’t hurt to file that as a bug report to them.

Regards,  
Florian

---

<div class="post-metadata">

**Author:** ![Xavier\_NoSSSSlle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/58f4c7/32.png) [@Xavier\_NoSSSSlle](https://www.ruby-forum.com/u/Xavier_NoSSSSlle)\
**Post date:** [January 21, 2010, 11:02am UTC](https://www.ruby-forum.com/t/multi-threaded-dbi-causing-segfault/181953/4 "2010-01-21T11:02:56Z")

</div>

2010/1/20 Roger P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > /usr/lib/ruby/1.8/dbd/mysql/statement.rb:36: [BUG] Segmentation fault  
> > ruby 1.8.7 (2009-06-12 patchlevel 174) [x86\_64-linux]
> > 
> > Question: is it a real bug or am I doing something wrong ?  
> > If I’m wrong, where ?  
> > If I’m not, is there a workaround to thread the execution of code  
> > using DBI without a segfault ?
> 
> Maybe use the mysql gem?

I think that’s the underlying gem used by DBI for dealing with MySQL,  
would it really help ?

> Also, you could run it in gdb to try to debug the cause.
> 
> [http://wiki.github.com/rdp/ruby\_tutorials\_core/building-mri#debug\_build](http://wiki.github.com/rdp/ruby_tutorials_core/building-mri#debug_build)

That was a good idea…in theory ! Because when I compile sources for  
the exact same version (and patchlevel) of Ruby, I don’t get the same  
error…then I tried to run gdb directly on the standard Ubuntu 9.10  
binary, but I think the error is not really helpful:

Program received signal SIGSEGV, Segmentation fault.  
0x00007ffff7b77517 in st\_foreach () from /usr/lib/libruby1.8.so.1.8
