Skip to content

Conversation

scottinet
Copy link
Contributor

@scottinet scottinet commented Jul 19, 2019

Description

Whenever a request is sent through AbstractProtocol.query:

  • a one-time listener is registered to the requestId event
  • a new entry is created in a map holding pending requests
  • a promise is created and returned to the caller

When an API response is received, its requestId is used to invoke the one-time listener which, in turns, remove the entry from the pending requests map, and resolves/rejects the promise.

But nothing was ever done if the network fails, meaning that requests were created with no hope of ever receiving a corresponding response.
This leads to several leaks, and it will also block users awaiting for SDK methods that might never resolve their returned promise.

This PR fixes the problem by making sure that AbstractProtocol.clear gets called on network disconnections (expected or not), ensuring that the pending requests map is properly cleared.
This PR also enhances the "clear" method by making it unregister the one-time event, and reject the corresponding promise, to make sure that no trace remains of the pending request.

This PR builds over #424 and fixes/enhances it.

@codecov
Copy link

codecov bot commented Jul 19, 2019

Codecov Report

Merging #427 into 6-dev will increase coverage by 0.03%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##            6-dev     #427      +/-   ##
==========================================
+ Coverage   96.41%   96.45%   +0.03%     
==========================================
  Files          32       33       +1     
  Lines        1535     1550      +15     
==========================================
+ Hits         1480     1495      +15     
  Misses         55       55
Impacted Files Coverage Δ
src/Kuzzle.js 94.59% <100%> (ø) ⬆️
src/protocols/abstract/pendingRequest.js 100% <100%> (ø)
src/protocols/abstract/realtime.js 100% <100%> (ø) ⬆️
src/protocols/abstract/common.js 91.83% <100%> (+0.72%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4f2ab1a...8caccc4. Read the comment docs.

sinon = require('sinon'),
KuzzleError = require('../../src/KuzzleError'),
AbstractWrapper = require('../../src/protocols/abstract/common');
KuzzleError = require(`${root}/src/KuzzleError`),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This kind of shortcut beak editor ability to automatically refactor imports when renaming files

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't know, I don't use IDEs 😑

I'll revert that one, since this makes code marginally more readable.

@Aschen Aschen merged commit 9d2347c into 6-dev Jul 22, 2019
@Aschen Aschen deleted the offline-fixes branch July 22, 2019 08:48
scottinet added a commit that referenced this pull request Jul 31, 2019
# [6.2.0](https://github.com/kuzzleio/sdk-javascript/releases/tag/6.2.0) (2019-07-31)


#### Bug fixes

- [ [#428](#428) ] Properly handle boolean flags in HTTP querystrings   ([scottinet](https://github.com/scottinet))
- [ [#427](#427) ] Solve promise+event+memory leaks when the network fails   ([scottinet](https://github.com/scottinet))
- [ [#424](#424) ] Prevent pending request leak when disconnect the SDK   ([Aschen](https://github.com/Aschen))
- [ [#422](#422) ] Fix bug when decoding JWT in browser   ([Aschen](https://github.com/Aschen))
- [ [#420](#420) ] Fix http protocol unresolved promise on connection error   ([Aschen](https://github.com/Aschen))

#### New features

- [ [#419](#419) ] Add bulk:write and bulk:mWrite   ([Aschen](https://github.com/Aschen))

#### Enhancements

- [ [#421](#421) ] Get api routes from server:publicApi   ([Aschen](https://github.com/Aschen))
- [ [#423](#423) ] Emit queryError event on malformed request   ([Aschen](https://github.com/Aschen))
- [ [#417](#417) ] Security controller documentation   ([benoitvidis](https://github.com/benoitvidis))
---
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants