Bug #4405


WIN32OLE & Threads incompatible

Added by larsch (Lars Christensen) almost 12 years ago. Updated almost 2 years ago.

Target version:
ruby -v:
ruby 1.9.2p136 (2010-12-25) [i386-mingw32]


The WIN32OLE library does not work when using Ruby threads. It may raise exceptions such as this:

(druby://localhost:2002) threadsys.rb:7:in connect': failed to parse display name of moniker winmgmts://localhost/root/cimv2' (WIN32OLERuntimeError)

WIN32 OLE api's are not generally Thread safe, and it can be argued that it is the user's task to ensure that it is accessed only from one thread, or the main thread. However, there are some complications;

  • Using WIN32OLE from DRb (DRb can not be used without Threads).
  • Using WIN32OLE indirectly (e.g. through Sys::ProcTable).

My specific case was a DRb server that examined processes using Sys::ProcTable, which happen to use WIN32OLE. This causes an exception. Attached is a simple script that recreates the problem. Note that in this simple case, WIN32OLE is only invokes once, and only from one single thread (and it still throws the exception).


threadsys.rb (257 Bytes) threadsys.rb Recreates the exception from WIN32OLE larsch (Lars Christensen), 02/17/2011 05:45 PM
Actions #1

Updated by headius (Charles Nutter) almost 12 years ago

We struggled with this a bit on JRuby too while implementing win32ole. The contract under which we implemented it also called for high concurrency, so we needed to figure out the right bits and pieces and threading models and so on.

Is it possible for you to try this with JRuby + jruby-win32ole gem? If it works, then what we did might help MRI.

Actions #2

Updated by rogerdpack (Roger Pack) almost 12 years ago

It does seem to work all right with jruby.

Updated by enebo (Thomas Enebo) almost 12 years ago

JRuby's implementation should be prone to the same problems when accessing an STA service from multiple threads. The fact that it works should probably be considered luck more than anything. I agree with the original reporters statement that thread-safety is the users problem to work around.

One thing we could change in win32ole is the ability to specify that we want to access the service as MTA. This would allow MT programs to work at the extreme performance penalty that comes along with accessing a STA as MTA (100-1000x slower). It doesn't crash and for many COM services performance is not an issue.

Actions #4

Updated by larsch (Lars Christensen) almost 12 years ago

Charles Nutter wrote:

Is it possible for you to try this with JRuby + jruby-win32ole gem? If it works, then what we did might help MRI.

Appears to work well with jruby 1.6.0.RC2 (ruby 1.8.7 patchlevel 330) & jruby-win32ole 0.8.3.

Actions #5

Updated by naruse (Yui NARUSE) over 11 years ago

  • Status changed from Open to Assigned
  • Assignee set to suke (Masaki Suketa)

Updated by jeremyevans0 (Jeremy Evans) almost 2 years ago

  • Status changed from Assigned to Closed

From my testing, this was fixed between Ruby 1.9.3 and Ruby 2.0, printing WIN32OLE object instead of raising an exception. It still works on Ruby 3.0, so I think this can be closed.


Also available in: Atom PDF