Skip to content
This repository has been archived by the owner on Dec 4, 2023. It is now read-only.

Extended support for context timeouts to function calls #287

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

dedene
Copy link

@dedene dedene commented Jan 5, 2014

Extended the protection against DoS introduced by @SamSaffron in #280 to function calls.

cxt = V8::Context.new timeout: 700
ctx.eval("var dos = function() { while(true){} };")
ctx['dos'].call #= exception after 700ms!

@SamSaffron
Copy link
Contributor

cool, I wonder if we can reused this code as opposed to having it cut-and-pasted in 2 spots?

@dedene
Copy link
Author

dedene commented Jan 6, 2014

I agree, however I'm not much of an expert in C.. Any ideas how we could extract the code

VALUE Function::[...]WithTimeout(..., VALUE timeout) {
    pthread_t breaker_thread;
    timeout_data data;
    VALUE rval;
    void *res;

    data.isolate = v8::Isolate::GetCurrent();
    data.timeout = NUM2LONG(timeout);

    pthread_create(&breaker_thread, NULL, rr::breaker, &data);

    rval = [...]

    pthread_cancel(breaker_thread);
    pthread_join(breaker_thread, &res);

    return rval;
  }

into a shared function?

@mattconnolly
Copy link

Can one of the admins verify this patch?

@SamSaffron
Copy link
Contributor

I think the fix is correct, however I am also uncertain about how to centralize this code @cowboyd any ideas?

@niko
Copy link

niko commented Oct 25, 2016

Wouldn't that be the solution to #395? Any progress here?

@SamSaffron
Copy link
Contributor

I have been through dealing with timeout bigtime with mini_racer, this gets nasty hairy, especially if you want to kick timeouts when an attached proc is running.

I ended up giving up using an unmanged breaker thread and instead use a pure ruby thread for it which ends up being significantly safer

@niko
Copy link

niko commented Oct 25, 2016

@SamSaffron how do you use a pure rb thread for that? Using Timeout::timeout()?

@SamSaffron
Copy link
Contributor

@niko I use: https://github.com/discourse/mini_racer/blob/master/lib/mini_racer.rb#L216-L236

This works great cause mini racer releases the GIL when it runs JS.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants