{"action":"created","issue":{"url":"https://api.github.com/repos/rpominov/kefir/issues/134","labels_url":"https://api.github.com/repos/rpominov/kefir/issues/134/labels{/name}","comments_url":"https://api.github.com/repos/rpominov/kefir/issues/134/comments","events_url":"https://api.github.com/repos/rpominov/kefir/issues/134/events","html_url":"https://github.com/rpominov/kefir/issues/134","id":103363632,"number":134,"title":"TypeError: Cannot read property 'shift' of null","user":{"login":"AgentME","id":577345,"avatar_url":"https://avatars.githubusercontent.com/u/577345?v=3","gravatar_id":"","url":"https://api.github.com/users/AgentME","html_url":"https://github.com/AgentME","followers_url":"https://api.github.com/users/AgentME/followers","following_url":"https://api.github.com/users/AgentME/following{/other_user}","gists_url":"https://api.github.com/users/AgentME/gists{/gist_id}","starred_url":"https://api.github.com/users/AgentME/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/AgentME/subscriptions","organizations_url":"https://api.github.com/users/AgentME/orgs","repos_url":"https://api.github.com/users/AgentME/repos","events_url":"https://api.github.com/users/AgentME/events{/privacy}","received_events_url":"https://api.github.com/users/AgentME/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":4,"created_at":"2015-08-26T21:03:59Z","updated_at":"2015-08-27T00:17:24Z","closed_at":null,"body":"In our application, we catch all uncaught exceptions and have the client log them to our server automatically. We've been seeing this many times a day from many users without much else in common:\r\n\r\nError logged: TypeError: Cannot read property 'shift' of null \r\n    at mixin.\\_init.\\_$shiftBuff ([src/one-source/delay.js:8](https://github.com/rpominov/kefir/blob/0339735b451a0ed0f93f59e0563f04dc4f8a390c/src/one-source/delay.js#L8))\r\n\r\nI've seen it happen a few times randomly, but I haven't been able to dependably reproduce the issue until today. My reproduction steps involved triggering a few certain bugs in the application while having Chrome's debugger set to pause on exceptions, and then this exception happens afterward. (I don't assume this is how users are triggering it.) To get more information, I modified delay.js's code to have a few lines added:\r\n\r\n```javascript\r\nconst mixin = {\r\n  //...\r\n  _free() {\r\n    this.__free_stack = new Error().stack; // +\r\n    this._buff = null;\r\n    this._$shiftBuff = null;\r\n  },\r\n  //...\r\n  _handleEnd() {\r\n    if (this._activating) {\r\n      this.__immediate_end = true; // +\r\n      this._emitEnd();\r\n    } else {\r\n      setTimeout(() => {\r\n        this.__timeout_end = true; // +\r\n        this._emitEnd();\r\n      }, this._wait);\r\n    }\r\n  }\r\n\r\n};\r\n```\r\n\r\nAt the moment of the exception, `this.__timeout_end` was true, and `this.__free_stack` contained:\r\n\r\nError \r\n    at AnonymousObservable._free ([src/one-source/delay.js:12](https://github.com/rpominov/kefir/blob/0339735b451a0ed0f93f59e0563f04dc4f8a390c/src/one-source/delay.js#L12)) \r\n    at AnonymousObservable._clear ([src/patterns/one-source.js:52](https://github.com/rpominov/kefir/blob/0339735b451a0ed0f93f59e0563f04dc4f8a390c/src/patterns/one-source.js#L52)) \r\n    at AnonymousObservable._emitEnd ([src/property.js:41](https://github.com/rpominov/kefir/blob/0339735b451a0ed0f93f59e0563f04dc4f8a390c/src/property.js#L41)) \r\n    at [src/one-source/delay.js:29](https://github.com/rpominov/kefir/blob/0339735b451a0ed0f93f59e0563f04dc4f8a390c/src/one-source/delay.js#L29)\r\n\r\nIt looks like the function assigned to `this._$shiftBuff` is being called after `this._emitEnd`. It looks like the code is relying on setTimeout callbacks with the same wait value being called in the order they're set, but this is not always the case: http://stackoverflow.com/a/5998338/1289657. If you view the example jsfiddle in Chrome, then you can easily trigger an error by hitting the pause and play buttons on the debugger a few times if one doesn't happen on its own.\r\n\r\nHere's an example test case which replaces setTimeout with a function more prone to executing events out of order and fails about 20% of the time with the \"Cannot read property 'shift' of null\" error.\r\n\r\n```javascript\r\nvar Kefir = require('kefir');\r\n\r\n(function() {\r\n  var originalST = global.setTimeout;\r\n  global.setTimeout = function() {\r\n    if (Math.random() > 0.5) {\r\n      return originalST.apply(this, arguments);\r\n    } else {\r\n      var _this = this, _arguments = arguments;\r\n      originalST(function() {\r\n        return originalST.apply(_this, _arguments);\r\n      }, 1);\r\n    }\r\n  };\r\n})();\r\n\r\nKefir.sequentially(0, [1,2,3]).delay(0).log();\r\n```"},"comment":{"url":"https://api.github.com/repos/rpominov/kefir/issues/comments/135215315","html_url":"https://github.com/rpominov/kefir/issues/134#issuecomment-135215315","issue_url":"https://api.github.com/repos/rpominov/kefir/issues/134","id":135215315,"user":{"login":"AgentME","id":577345,"avatar_url":"https://avatars.githubusercontent.com/u/577345?v=3","gravatar_id":"","url":"https://api.github.com/users/AgentME","html_url":"https://github.com/AgentME","followers_url":"https://api.github.com/users/AgentME/followers","following_url":"https://api.github.com/users/AgentME/following{/other_user}","gists_url":"https://api.github.com/users/AgentME/gists{/gist_id}","starred_url":"https://api.github.com/users/AgentME/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/AgentME/subscriptions","organizations_url":"https://api.github.com/users/AgentME/orgs","repos_url":"https://api.github.com/users/AgentME/repos","events_url":"https://api.github.com/users/AgentME/events{/privacy}","received_events_url":"https://api.github.com/users/AgentME/received_events","type":"User","site_admin":false},"created_at":"2015-08-27T00:17:24Z","updated_at":"2015-08-27T00:17:24Z","body":"I found a way to fix it that isn't a massive hack and put in a PR. Even if there weren't any setTimeout-related bugs, this code would still be a sensible implementation of delay."}}