# \[patch\] Fix ruby-glib thread handling

**URL:** <https://www.ruby-forum.com/t/patch-fix-ruby-glib-thread-handling/133347>\
**Category:** Ruby-Gnome 2\
**Created:** [March 30, 2008, 4:23pm UTC](https://www.ruby-forum.com/t/patch-fix-ruby-glib-thread-handling/133347 "2008-03-30T16:23:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sschunselaar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/34f0e0/32.png) [@sschunselaar](https://www.ruby-forum.com/u/sschunselaar)\
**Post date:** [March 30, 2008, 4:23pm UTC](https://www.ruby-forum.com/t/patch-fix-ruby-glib-thread-handling/133347/1 "2008-03-30T16:23:23Z")

</div>

Hi,

Attached patch fixes the way ruby-glib handles signals from threads.  
This  
means it reverts to using a pipe like system so the main ruby thread  
can  
select on it to ensure it runs when needed. The main difference that  
is that  
we use IO.pipe instead of pipe() to create the pipe, which should make  
the  
code portable. I also added a test-case showing the effect, it would  
be nice  
if someone could test this on a win32 system just to be sure

Sjoerd

---

<div class="post-metadata">

**Author:** ![sschunselaar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/34f0e0/32.png) [@sschunselaar](https://www.ruby-forum.com/u/sschunselaar)\
**Post date:** [March 30, 2008, 7:57pm UTC](https://www.ruby-forum.com/t/patch-fix-ruby-glib-thread-handling/133347/2 "2008-03-30T19:57:32Z")

</div>

> Attached patch fixes the way ruby-glib handles signals from threads. This

> From Ruby threads, you mean? Is there any evidence that calling Gtk  
> stuff from Ruby threads is now safe? It has never been, for as long as  
> I’ve been using rg2 (several years) - check archives for details if  
> needed…

–  
Guillaume C. - [Guillaume Cottenceau](http://zarb.org/~gc/)

---

<div class="post-metadata">

**Author:** ![sschunselaar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/34f0e0/32.png) [@sschunselaar](https://www.ruby-forum.com/u/sschunselaar)\
**Post date:** [March 30, 2008, 8:21pm UTC](https://www.ruby-forum.com/t/patch-fix-ruby-glib-thread-handling/133347/3 "2008-03-30T20:21:15Z")

</div>

On Sun, Mar 30, 2008 at 07:56:38PM +0200, Guillaume C. wrote:

> > Attached patch fixes the way ruby-glib handles signals from threads. This
> 
> > From Ruby threads, you mean? Is there any evidence that calling Gtk  
> > stuff from Ruby threads is now safe? It has never been, for as long as  
> > I’ve been using rg2 (several years) - check archives for details if  
> > needed…

This isn’t about calling Gtk stuff from ruby threads. This is for  
handling  
signals from native threads, which tends to happen in gstreamer programs  
reasonly often. What you do in the actual callback still has to be safe.  
Gstreamer things are fine, Gtk things are usually problematic indeed.

But yeah, native vs. ruby threads are confusing from time when  
describing stuff  
🙂

## Sjoerd

The herd instinct among economists makes sheep look like independent  
thinkers.

---

<div class="post-metadata">

**Author:** ![sschunselaar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/34f0e0/32.png) [@sschunselaar](https://www.ruby-forum.com/u/sschunselaar)\
**Post date:** [March 31, 2008, 5:07pm UTC](https://www.ruby-forum.com/t/patch-fix-ruby-glib-thread-handling/133347/4 "2008-03-31T17:07:28Z")

</div>

Hi,

In [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
“[ruby-gnome2-devel-en] [patch] Fix ruby-glib thread handling” on Sun,  
30 Mar 2008 16:22:59 +0200,  
Sjoerd S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Attached patch fixes the way ruby-glib handles signals from threads. This  
> means it reverts to using a pipe like system so the main ruby thread can  
> select on it to ensure it runs when needed. The main difference that is that  
> we use IO.pipe instead of pipe() to create the pipe, which should make the  
> code portable. I also added a test-case showing the effect, it would be nice  
> if someone could test this on a win32 system just to be sure

It’s simple that we use \_pipe() on Windows rather than using  
IO.pipe.

Thanks,

---

<div class="post-metadata">

**Author:** ![sschunselaar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/34f0e0/32.png) [@sschunselaar](https://www.ruby-forum.com/u/sschunselaar)\
**Post date:** [April 5, 2008, 5:42pm UTC](https://www.ruby-forum.com/t/patch-fix-ruby-glib-thread-handling/133347/5 "2008-04-05T17:42:11Z")

</div>

On Tue, Apr 01, 2008 at 12:06:47AM +0900, Kouhei S. wrote:

> > code portable. I also added a test-case showing the effect, it would be nice  
> > if someone could test this on a win32 system just to be sure
> 
> It’s simple that we use \_pipe() on Windows rather than using  
> IO.pipe.

Ah nice, i’ll do something similar in the threaded gstreamer calls  
then. The  
only problem with your patch i saw is that you do the pipe() call  
inside the  
created ruby thread. Which means it’s not guaranteed that it will be  
done  
when the first signal comes in (or at least afaik rb\_thread\_create  
does not  
guarantee the created thread will be executed first). So a little fix  
for  
that attached.

Also stopping the callback thread is not really thread safe. But it’s  
not  
that important imho, so i didn’t spent time on fixing that 🙂 (It also  
wasn’t  
threadsafe in the patch i attached earlier btw)

```
Sjoerd
```

---

<div class="post-metadata">

**Author:** ![sschunselaar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/34f0e0/32.png) [@sschunselaar](https://www.ruby-forum.com/u/sschunselaar)\
**Post date:** [April 5, 2008, 5:59pm UTC](https://www.ruby-forum.com/t/patch-fix-ruby-glib-thread-handling/133347/6 "2008-04-05T17:59:54Z")

</div>

Hi,

In [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
“Re: [ruby-gnome2-devel-en] [patch] Fix ruby-glib thread handling” on  
Sat, 5 Apr 2008 17:40:56 +0200,  
Sjoerd S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > we use IO.pipe instead of pipe() to create the pipe, which should make the  
> > > guarantee the created thread will be executed first). So a little fix for  
> > > that attached.

Please commit your patch.

## Thanks,

kou
