<html>
    <head>
      <base href="https://bugs.webkit.org/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Incorrect behavior when patching window.setTimeout"
   href="https://bugs.webkit.org/show_bug.cgi?id=143836">143836</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Incorrect behavior when patching window.setTimeout
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>WebKit
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>528+ (Nightly build)
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>Unspecified
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Unspecified
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>Normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P2
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>WebCore JavaScript
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>webkit-unassigned&#64;lists.webkit.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>mmanela&#64;gmail.com
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=250935" name="attach_250935" title="File which demonstrates the issue">attachment 250935</a> <a href="attachment.cgi?id=250935&amp;action=edit" title="File which demonstrates the issue">[details]</a></span>
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;); }</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are the assignee for the bug.</li>
      </ul>
    </body>
</html>