Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stuck in while loop indefinitely #72

Closed
JohnyDays opened this issue Oct 3, 2016 · 10 comments
Closed

Stuck in while loop indefinitely #72

JohnyDays opened this issue Oct 3, 2016 · 10 comments
Labels

Comments

@JohnyDays
Copy link

I found a minimal repro case, it always gets stuck in the 27th cycle (script never ends)

var expression = "0 12 * * *"
var cron = require("cron-parser")
var currentDate = new Date().toISOString()

var iterator = cron.parseExpression(expression, {currentDate:currentDate})

for (var i=0; i<40;i++) {
  console.log(iterator.next().getTime())
}

I have also tried to recreate the parser on every iteration of the loop, but the results were the same.
Any idea what it might be?
Thanks

@santigimeno
Copy link
Contributor

It looks again a DST transition issue. Will look into it when time permits

@santigimeno
Copy link
Contributor

Thanks for the report

@JohnyDays
Copy link
Author

JohnyDays commented Oct 3, 2016

Can i somehow disable timezone handling? I don't need it for this particular case
EDIT: Might also be relevant, my timezone is Lisbon (GMT+1 currently because of DST)

@santigimeno
Copy link
Contributor

I would use the latest 1.x.x version.

@JohnyDays
Copy link
Author

That seems to work perfectly, thanks! If you need some more information when debugging the issue just hit me up! 👍

@harrisiirak
Copy link
Owner

@santigimeno @JohnyDays a good catch. Let me know how this is going. Thanks for the reporting @JohnyDays!

@santigimeno
Copy link
Contributor

I've been looking a little into this. Changing your code to use moment with timezone information should do it:

var expression = "0 12 * * *"
var cron = require("cron-parser")
var moment = require('moment-timezone');
var currentDate = moment().tz('Europe/Lisbon');

var iterator = cron.parseExpression(expression, {currentDate:currentDate})

for (var i=0; i<40;i++) {
  console.log(iterator.next().toString());
}

@santigimeno
Copy link
Contributor

For the issue itself, I have reduced the case to this. It's due to how moment.startOf() behaves. I don't know if it's a bug or it works as expected.

'use strict';

const moment = require('moment-timezone');

var m = moment(new Date('Sun Oct 30 2016 02:00:00 GMT+0200'));
console.log(m.toString(), '<-- Just 1 hour before the DST end @ Europe/Madrid');
m.add(1, 'hours');
console.log(m.toString(), '<-- DST end transition @ Europe/Madrid works correctly');
m.startOf('hour');
console.log(m.toString(), '<-- Smthing wrong?? It goes back to the previous hour');

It's output being:

Sun Oct 30 2016 02:00:00 GMT+0200 <-- Just 1 hour before the DST end @ Europe/Madrid
Sun Oct 30 2016 02:00:00 GMT+0100 <-- DST end transition @ Europe/Madrid works correctly
Sun Oct 30 2016 02:00:00 GMT+0200 <-- Smthing wrong?? It goes back to the previous hour

OTOH, if the timezone is explicitly used, it works as expected

'use strict';

const moment = require('moment-timezone');

var m = moment(new Date('Sun Oct 30 2016 02:00:00 GMT+0200')).tz('Europe/Madrid');
console.log(m.toString(), '<-- Just 1 hour before the DST end @ Europe/Madrid');
m.add(1, 'hours');
console.log(m.toString(), '<-- DST end transition @ Europe/Madrid works correctly');
m.startOf('hour');
console.log(m.toString(), '<-- Everything correct. It remains in the same hour');

It's output being

Sun Oct 30 2016 02:00:00 GMT+0200 <-- Just 1 hour before the DST end @ Europe/Madrid
Sun Oct 30 2016 02:00:00 GMT+0100 <-- DST end transition @ Europe/Madrid works correctly
Sun Oct 30 2016 02:00:00 GMT+0100 <-- Everything correct. It remains in the same hour

Thoughts? /cc @harrisiirak @mj1856

@santigimeno
Copy link
Contributor

It looks like this issue: moment/moment#2749

santigimeno added a commit to santigimeno/cron-parser that referenced this issue Oct 8, 2016
On DST end transition, setting the date to the `startOf` `hour`,
`minute` or `second` after having added 1 `hour`, `minute` or `second`
to the date, would result in the date set to the initial value causing
an infinite loop.

Example:

```js
'use strict';

const moment = require('moment-timezone');

var m = moment(new Date('Sun Oct 30 2016 02:00:00 GMT+0200'));
console.log(m.toString(),
            '<-- Just 1 hour before the DST end @ Europe/Madrid');
m.add(1, 'hours');
console.log(m.toString(),
            '<-- DST end transition @ Europe/Madrid works correctly');
m.startOf('hour');
console.log(m.toString(),
            '<-- Smthing wrong?? It goes back to the previous hour');
```

Fixes: harrisiirak#72
santigimeno added a commit to santigimeno/cron-parser that referenced this issue Oct 8, 2016
On DST end transition, setting the date to the `startOf` `hour`,
`minute` or `second` after having added 1 `hour`, `minute` or `second`
to the date, would result in the date set to the initial value causing
an infinite loop.

Example:

```js
'use strict';

const moment = require('moment-timezone');

var m = moment(new Date('Sun Oct 30 2016 02:00:00 GMT+0200'));
console.log(m.toString(),
            '<-- Just 1 hour before the DST end @ Europe/Madrid');
m.add(1, 'hours');
console.log(m.toString(),
            '<-- DST end transition @ Europe/Madrid works correctly');
m.startOf('hour');
console.log(m.toString(),
            '<-- Smthing wrong?? It goes back to the previous hour');
```

Fixes: harrisiirak#72
santigimeno added a commit to santigimeno/cron-parser that referenced this issue Oct 8, 2016
On DST end transition, setting the date to the `startOf` `hour`,
`minute` or `second` after having added 1 `hour`, `minute` or `second`
to the date, would result in the date set to the initial value causing
an infinite loop.

Example:

```js
'use strict';

const moment = require('moment-timezone');

var m = moment(new Date('Sun Oct 30 2016 02:00:00 GMT+0200'));
console.log(m.toString(),
            '<-- Just 1 hour before the DST end @ Europe/Madrid');
m.add(1, 'hours');
console.log(m.toString(),
            '<-- DST end transition @ Europe/Madrid works correctly');
m.startOf('hour');
console.log(m.toString(),
            '<-- Smthing wrong?? It goes back to the previous hour');
```

Fixes: harrisiirak#72
Ref: moment/moment#2749
@santigimeno
Copy link
Contributor

Tentative fix #73. /cc @harrisiirak @JohnyDays

santigimeno added a commit to santigimeno/cron-parser that referenced this issue Oct 15, 2016
On DST end transition, setting the date to the `startOf` `hour`,
`minute` or `second` after having added 1 `hour`, `minute` or `second`
to the date, would result in the date set to the initial value causing
an infinite loop.

Example:

```js
'use strict';

const moment = require('moment-timezone');

var m = moment(new Date('Sun Oct 30 2016 02:00:00 GMT+0200'));
console.log(m.toString(),
            '<-- Just 1 hour before the DST end @ Europe/Madrid');
m.add(1, 'hours');
console.log(m.toString(),
            '<-- DST end transition @ Europe/Madrid works correctly');
m.startOf('hour');
console.log(m.toString(),
            '<-- Smthing wrong?? It goes back to the previous hour');
```

Fixes: harrisiirak#72
Ref: moment/moment#2749
santigimeno added a commit to santigimeno/cron-parser that referenced this issue Oct 15, 2016
On DST end transition, setting the date to the `startOf` `hour`,
`minute` or `second` after having added 1 `hour`, `minute` or `second`
to the date, would result in the date set to the initial value causing
an infinite loop.

Example:

```js
'use strict';

const moment = require('moment-timezone');

var m = moment(new Date('Sun Oct 30 2016 02:00:00 GMT+0200'));
console.log(m.toString(),
            '<-- Just 1 hour before the DST end @ Europe/Madrid');
m.add(1, 'hours');
console.log(m.toString(),
            '<-- DST end transition @ Europe/Madrid works correctly');
m.startOf('hour');
console.log(m.toString(),
            '<-- Smthing wrong?? It goes back to the previous hour');
```

Fixes: harrisiirak#72
Ref: moment/moment#2749
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants