Skip to content

bug: bolt does not trigger handlers consistently when handler pattern contains stateful regex flags (/y or /g) #1058

@kefniark

Description

@kefniark

Description

Hi There,

Not really a Bolt-js issue, more an unexpected behavior and bad code from the net. You can close this issue when you want.

It took me quite some time to figure what was happening, so I'm posting it there if anyone run into the same kind of issue.

Problem:

  • calling a command /hello a first time works
  • calling the same command a second time ... timeout and /hello failed with the error "operation_timeout"
  • the server log only show one line, pointing to a ack() issue [ERROR] An incoming event was not acknowledged within 3 seconds. Ensure that the ack() argument is called in a listener.
  • after waiting a bit, I can run the command again, but only once
  • if I run another command, it works once, then timeout too
  • when it timeout, it doesnt even reach my listener or any part of my bolt code
  app.command(/(\/hello-dev|\/hello)/g, async ({ command, ack, respond }) => {
    await ack()
    await sayHello(command, respond)
  })

Solution:
After debugging all over bolt and expressReceiver, I figured that the culprit is around here

The command regexp is tested with return pattern.test(candidate); and any flag immediately become problematic.
My bad for copying a bad regexp from a bolt tutorial without noticing that.

So changing the regexp suddenly solved the problem (getting rid of flags like /g, /y)

app.command(/^\/(hello-dev|hello).*/, async ({ command, ack, respond }) => {
    await ack()
    await sayHello(command, respond)
})

Idea for improvement:

  • a warning message if not a single listener match, it would be way more helpful than a timeout
  • not showing a warning related to ack() if not a single listener match
  • a check on .lastIndex or .flags to check that regexp are not carrying a state over queries

What type of issue is this? (place an x in one of the [ ])

  • bug
  • enhancement (feature request)
  • question
  • documentation related
  • example code related
  • testing related
  • discussion

Requirements (place an x in each of the [ ])

  • I've read and understood the Contributing guidelines and have done my best effort to follow them.
  • I've read and agree to the Code of Conduct.
  • I've searched for any related issues and avoided creating a duplicate issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    auto-triage-skipPrevent this issue from being closed due to lack of activitybugM-T: confirmed bug report. Issues are confirmed when the reproduction steps are documented

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions