<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://bugs.webkit.org/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4.1"
          urlbase="https://bugs.webkit.org/"
          
          maintainer="admin@webkit.org"
>

    <bug>
          <bug_id>143836</bug_id>
          
          <creation_ts>2015-04-16 11:55:21 -0700</creation_ts>
          <short_desc>Incorrect behavior when patching window.setTimeout</short_desc>
          <delta_ts>2015-04-20 11:53:26 -0700</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WebKit</product>
          <component>JavaScriptCore</component>
          <version>528+ (Nightly build)</version>
          <rep_platform>Unspecified</rep_platform>
          <op_sys>Unspecified</op_sys>
          <bug_status>NEW</bug_status>
          <resolution></resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords></keywords>
          <priority>P2</priority>
          <bug_severity>Normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Matthew Manela">mmanela</reporter>
          <assigned_to name="Nobody">webkit-unassigned</assigned_to>
          <cc>ap</cc>
    
    <cc>ggaren</cc>
    
    <cc>sean</cc>
    
    <cc>zackw</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>1086002</commentid>
    <comment_count>0</comment_count>
      <attachid>250935</attachid>
    <who name="Matthew Manela">mmanela</who>
    <bug_when>2015-04-16 11:55:21 -0700</bug_when>
    <thetext>Created attachment 250935
File which demonstrates the issue

This is a demonstration of a really strange issue in Webkit (and PhantomJS which uses Webkit). The issue involves what happens when you monkey patch the window.setTimeout method in Javascript. The issue was discovered when debugging test failures that were using sinonJS (which monkey patches setTimeout). I worked through that issue and distilled the minimum repro out of it.

I validated this issue does not occur in Chrome 41, Firefox 36 and IE 11. As far as I can tell it only repros in Webkit based browsers (like Phantomjs).

Short Repro

1: Define a method (check) which references window.setTimeout

2: Call this method two times, then patch setTimeout to be a custom method and then call check again.

CODE:

function check() {
    console.log(&quot;window.setTimeout = &quot; + window.setTimeout);
}
check();
check();
window.setTimeout = function() { console.log (&quot;Patched&quot;); }
check();
OUTPUT:

window.setTimeout = function setTimeout() {
    [native code]
}

window.setTimeout = function setTimeout() {
    [native code]
}

window.setTimeout = function setTimeout() {
    [native code]
}
3: Then call to check the third time will not output the contents of the monkey patched function. It will still contain the native one. THe odd thing is if you call the check() method only once (or not at all) before your patched setTimeout then it will output the expected value. For example:

CODE:

function check() {
    console.log(&quot;window.setTimeout = &quot; + window.setTimeout);
}
check();
window.setTimeout = function() { console.log (&quot;Patched&quot;); }
check();
OUTPUT:

window.setTimeout = function setTimeout() {
    [native code]
}

window.setTimeout = function () { console.log(&quot;PATCHED&quot;); }</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1086183</commentid>
    <comment_count>1</comment_count>
    <who name="Alexey Proskuryakov">ap</who>
    <bug_when>2015-04-16 17:21:37 -0700</bug_when>
    <thetext>I cannot reproduce this in Safari 7.1.5, nor in a current WebKit build.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1086310</commentid>
    <comment_count>2</comment_count>
    <who name="Matthew Manela">mmanela</who>
    <bug_when>2015-04-17 06:46:51 -0700</bug_when>
    <thetext>Thanks for the comment. I reproed this on windows using PhantomJS which is build on top of QtWebKit. I checked what version of Webkit that is using and it said the following in QtWebKit:


&quot;&quot;&quot;
   This is a snapshot of WebKit. It is based on the upstream trunk subversion
revision 153112

You can browse the base of this snapshot at

    http://trac.webkit.org/browser/trunk?rev=153112

Additional patches may have been applied on top and files not required by the
Qt port may have been removed.
&quot;&quot;&quot;</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1086700</commentid>
    <comment_count>3</comment_count>
    <who name="Matthew Manela">mmanela</who>
    <bug_when>2015-04-19 17:04:51 -0700</bug_when>
    <thetext>Also, I hit this issue on Windows. Could there be an OS specific bug?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1086827</commentid>
    <comment_count>4</comment_count>
    <who name="Zack Weinberg">zackw</who>
    <bug_when>2015-04-20 11:15:56 -0700</bug_when>
    <thetext>Over at https://github.com/ariya/phantomjs/issues/13158 we are able to reproduce the problem in both Midori and PhantomJS, but only on Windows.</thetext>
  </long_desc>
      
          <attachment
              isobsolete="0"
              ispatch="0"
              isprivate="0"
          >
            <attachid>250935</attachid>
            <date>2015-04-16 11:55:21 -0700</date>
            <delta_ts>2015-04-16 11:55:21 -0700</delta_ts>
            <desc>File which demonstrates the issue</desc>
            <filename>test[1].html</filename>
            <type>text/html</type>
            <size>1492</size>
            <attacher name="Matthew Manela">mmanela</attacher>
            
              <data encoding="base64">CjwhRE9DVFlQRSBodG1sPgo8aHRtbD4KPGhlYWQ+CiAgICA8bWV0YSBjaGFyc2V0PSJ1dGYtOCIg
Lz4KICAgIDx0aXRsZT5TZXRUaW1lb3V0IFBoYW50b20gUmVwcm88L3RpdGxlPgoKPHNjcmlwdCB0
eXBlPSJ0ZXh0L2phdmFzY3JpcHQiPgoJCgovLyBUaGlzIG1ldGhvZCBpcyBjYWxsZWQgMyB0aW1l
cwpmdW5jdGlvbiBjaGVjazEoKSB7CiAgICBjb25zb2xlLmxvZygiY2hlY2sxOiB3aW5kb3cuc2V0
VGltZW91dCA9ICIgKyB3aW5kb3cuc2V0VGltZW91dCk7Cn0KCgoKLy8gVGhpcyBtZXRob2QgaXMg
Y2FsbGVkIDIgdGltZXMKZnVuY3Rpb24gY2hlY2syKCkgewogICAgY29uc29sZS5sb2coImNoZWNr
Mjogd2luZG93LnNldFRpbWVvdXQgPSAiICsgd2luZG93LnNldFRpbWVvdXQpOwp9CgoKLy8gVGhp
cyBtZXRob2Qgd2lsbCBiZSBjYWxsZWQgMSB0aW1lCmZ1bmN0aW9uIGNoZWNrMygpIHsKICAgIGNv
bnNvbGUubG9nKCJjaGVjazM6IHdpbmRvd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