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

Aborted requests leak AMT Channels #743

Closed
orinem opened this issue Nov 26, 2022 · 0 comments · Fixed by #744
Closed

Aborted requests leak AMT Channels #743

orinem opened this issue Nov 26, 2022 · 0 comments · Fixed by #744

Comments

@orinem
Copy link
Contributor

orinem commented Nov 26, 2022

Describe the bug 🪲

If an API request to MPS is aborted for whatever reason (browser 'reload', browser window/tab closed, 'back' in the browser?), the AMT channel being used by that request isn't closed. Eventually, the device runs out of channels and API calls fail.

To Reproduce 🪜
Steps to reproduce the behavior:

Set silly level logging.
On a device page, hit reload several times before the page finishes loading. Some API failures messages will probably show briefly.

Note in the MPS logs that there are completed API calls without an associated channel close.

Expected behavior

AMT channels should be closed immediately after an API call completes.

AMT Device (please complete the following information): 🖥️

  • OS: N/A
  • AMT Version: N/A
  • AMT Configuration Mode: N/A
  • Network Configuration N/A

Service Deployment (please complete the following information): ⛈️

  • Deployment Type: Docker
  • Node Version: 16
  • Component & Version: 2.6.0

Additional context

Note that eventually, the device will timeout the orphaned channels and reloading the device page will succeed.

MPS is relying on either of the 'close' and 'finish' events being emitted by the Response object to close the AMT channel. In this case, neither are emitted.

The only event I've found to be emitted is 'aborted' on the Request object.

The obvious fix is to also close the channel on an aborted request, something like:

  appUseCall (req: Request, res: Response, next: NextFunction): void {
    res.on('finish', this.afterResponse.bind(this, req, res))
    res.on('close', this.afterResponse.bind(this, req, res))
    req.on('aborted', this.onAborted.bind(this, req, res))
    next()
  }

  onAborted (req: Request, res: Response): void {
    logger.debug(`Request aborted: ${req.url ?? 'undefined'}`)
    this.afterResponse(req, res)
  }

  afterResponse (req: Request, res: Response): void {
    if (req.deviceAction?.ciraHandler?.channel) {
      logger.debug(messages.EOR_CLOSING_CHANNEL)
      req.deviceAction.ciraHandler.channel.CloseChannel()
    }
    res.removeListener('finish', this.afterResponse)
    res.removeListener('close', this.afterResponse)
    req.removeListener('aborted', this.onAborted)
    // actions after response
  }

However, on reviewing nodejs issues re: which events are emitted when, I could not trust that this is the only event we might miss resulting in leaking AMT channels, or even that the 'aborted' event on the Request object is how they'll handle such cases in the future.

A better solution IMO is to add:

    finally {
      try {
        req.deviceAction?.ciraHandler?.channel?.CloseChannel()
      } catch (error) {
        logger.error(`${messages.ALARM_OCCURRENCES_EXCEPTION} : ${error}`)
      }  
    }

or similar to the end of each API, after the existing try-catch block to ensure any channel created would be properly closed. It is rather tedious though.

orinem added a commit to orinem/mps that referenced this issue Nov 27, 2022
orinem added a commit to orinem/mps that referenced this issue Nov 27, 2022
…rted' event

Ensure channel is closed if an API request is aborted.
Improved afterResponse test to ensure CloseChannel is called.
orinem added a commit to orinem/mps that referenced this issue Nov 28, 2022
…rted' event

Ensure channel is closed if an API request is aborted.
Improved afterResponse test to ensure CloseChannel is called.
@rsdmike rsdmike linked a pull request Nov 28, 2022 that will close this issue
1 task
rsdmike pushed a commit that referenced this issue Nov 28, 2022
Ensure channel is closed if an API request is aborted.
Improved afterResponse test to ensure CloseChannel is called.
@matt-primrose matt-primrose added this to the December Release milestone Jan 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants