Thread takeover

A

Andre'

Strangely, I'm having the problem that one thread is taking over the whole
process.

Imagine this main file:

Thread.new {
class Main
def initialize
while true
print 1
end
end
end
m = Main.new
}
Thread.new {
require 'p.rb'
m = Main.new
}
while true; end

It has two threads: one is printing 1 all the time in the screen. I have a
second - external - that is exactly like the first, but it prints 2. The
'p.rb' is this one:

class Main
def initialize
while true
print 2
end
end
end

It acts as expected. (11112222221111111222222)

But if I add the line "m = Main.new" at the end of 'p.rb', the thread
seems to take over! (22222222222222222222222222222222222)

Since this 'p.rb' is some external file the users will input, I can't tell
what it'll be on it, so I can't avoid that a malicious code takes over my
program! Or can I?
 
R

Robert Klemme

Andre' said:
Strangely, I'm having the problem that one thread is taking over the whole
process.

Imagine this main file:

Thread.new {
class Main
def initialize
while true
print 1
end
end
end
m = Main.new
}
Thread.new {
require 'p.rb'
m = Main.new
}
while true; end

Some remarks:

- Thread#join is far better than "while true; end", because does not burn
CPU resources. Apart from that the endless loop might behave bad with
regard to thread scheduling

- better define classes at top level

- don't do processing in the constructor

- don't use require for importing files that may change, require will
only load once
It has two threads: one is printing 1 all the time in the screen. I have a
second - external - that is exactly like the first, but it prints 2. The
'p.rb' is this one:

class Main
def initialize
while true
print 2
end
end
end

It acts as expected. (11112222221111111222222)

But if I add the line "m = Main.new" at the end of 'p.rb', the thread
seems to take over! (22222222222222222222222222222222222)

That's probably a scoping issue; it might be that Main#initialize in p.rb
overwrites Main#initialize in your script file.
Since this 'p.rb' is some external file the users will input, I can't tell
what it'll be on it, so I can't avoid that a malicious code takes over my
program! Or can I?

You can't if you simply require arbitrary code. The interpreter will
happily execute whatever he finds in p.rb. You can limit damage by
setting $SAFE in the thread loading the code plus taking some more
measures but I'm not sure whether you can really stop threads from running
berserk. (I believe not.)

Kind regards

robert
 
A

Andre'

Thank you for the hints.
That's probably a scoping issue; it might be that Main#initialize in p.rb
overwrites Main#initialize in your script file.

I don't think so. If I change the name of the class in one of the classes,
I still get the same problem.
You can't if you simply require arbitrary code. The interpreter will
happily execute whatever he finds in p.rb. You can limit damage by
setting $SAFE in the thread loading the code plus taking some more
measures but I'm not sure whether you can really stop threads from
running
berserk. (I believe not.)

I tried. Setting $SAFE higher will only disallow loading a file (which I
can't) and it won't solve my problem.

André
 
G

Gennady Bystritsky

I would suggest that the problem is that you loop in Main's
constructor, so that Main.new operation never ends. I suspect that new
is implemented inside ruby interpreter as C code, and it is a well a
known fact that entering C code takes over the whole interpreter.
That's why it is so important to remember when writing C extensions.
Here, however, you simply got in the way of the interpreter putting all
your processing in the constructor.

It is just a guess. I did not check it in the code, so I may be totally
wrong.

Gennady.

Strangely, I'm having the problem that one thread is taking over the
whole process.

Imagine this main file:

Thread.new {
class Main
def initialize
while true
print 1
end
end
end
m = Main.new
}
Thread.new {
require 'p.rb'
m = Main.new
}
while true; end

It has two threads: one is printing 1 all the time in the screen. I
have a second - external - that is exactly like the first, but it
prints 2. The 'p.rb' is this one:

class Main
def initialize
while true
print 2
end
end
end

It acts as expected. (11112222221111111222222)

But if I add the line "m = Main.new" at the end of 'p.rb', the thread
seems to take over! (22222222222222222222222222222222222)

Since this 'p.rb' is some external file the users will input, I can't
tell what it'll be on it, so I can't avoid that a malicious code takes
over my program! Or can I?

Sincerely,
Gennady Bystritsky
 
Y

YANAGAWA Kazuhisa

In Message-Id: <opscknzpr3fqyrmb@orquidia>
Andre' said:
But if I add the line "m = Main.new" at the end of 'p.rb', the thread
seems to take over! (22222222222222222222222222222222222)

On require, Ruby stops thread scheduling and probably GC until the end
of require. So, that's normal.

Since this 'p.rb' is some external file the users will input, I can't
tell what it'll be on it, so I can't avoid that a malicious code
takes over my program! Or can I?

If p.rb is a personal config file, whatever occurred is writers sake :)
If p.rb is required by several people or distributed via network,
better not just require it and develop more secure and robust scheme.
 
A

Andre'

I think I found out the problem. I'm using Win32, and I was using the
DJGPP compilation of Ruby. In the MINGW compilation, it works smooth.
Haven't tested it on Linux, though... but it should work.

André
 
Y

YANAGAWA Kazuhisa

In Message-Id: <[email protected]>
YANAGAWA Kazuhisa said:
On require, Ruby stops thread scheduling and probably GC until the end
of require. So, that's normal.

I'm sorry that I wrote wrong info by my own thought. As other posts
in this thread say, that's not a normal semantics.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,155
Messages
2,570,871
Members
47,401
Latest member
CliffGrime

Latest Threads

Top