Fix message ports not being closed when proxy is relased#6
Conversation
| propProxyCache.clear(); | ||
| unregisterProxy(proxy); | ||
| releaseEndpoint(ep); | ||
| pendingListeners.clear(); |
There was a problem hiding this comment.
Is the issue that clearing the listeners here is premature because the releaseEndpoint call needs to access the listeners to resolve? If that's the case it looks like releaseEndpoint itself returns a promise so you could do a .finally(() => pendingListeners.clear()) here instead of passing it to releaseEndpoint?
Or is it important to pass pendingListeners so it can be passed to requestResponseMessage ?
There was a problem hiding this comment.
Or is it important to pass
pendingListenersso it can be passed torequestResponseMessage?
Yes, that's the important bit. If pendingListeners are not passed, the resolver function is not found and the requestResponseMessage promise will never settle.
There was a problem hiding this comment.
So why do we allow pendingListeners to be undefined? In what situation is that ok?
There was a problem hiding this comment.
This was necessary when releasing the endpoint when the proxy is garbage collected. However, thinking about this more, we should also have access to the pendingListeners map as it is tightly coupled to the endpoint now.
There was a problem hiding this comment.
I have added a commit that changes the endpoint and pendingListeners to be passed around in a single object (EndpointWithPendingListeners). There is probably a better name for that type/object...
defunctzombie
left a comment
There was a problem hiding this comment.
Not sure why it is ok for pendingListeners to be false if we need it to invoke releaseEndpoint correctly - some comments on expected behaviors and implementation details would go a long way towards making this easier to maintain
…on registry cleanup
| proxy: object, | ||
| epWithPendingListeners: EndpointWithPendingListeners | ||
| ) { | ||
| const newCount = (proxyCounter.get(epWithPendingListeners) || 0) + 1; |
There was a problem hiding this comment.
| const newCount = (proxyCounter.get(epWithPendingListeners) || 0) + 1; | |
| const newCount = (proxyCounter.get(epWithPendingListeners) ?? 0) + 1; |
There was a problem hiding this comment.
I'm leaving this as is as the logical OR operator works with much older browser versions. The nullish coalescing operator was introduced much later.
There was a problem hiding this comment.
fwiw - that is handled during the transpilation step of ts -> js. When you specify a specific ES version as the target it will produce the code for that ES version so you can write your ts using modern styles.
Changelog
Fix message ports not being closed when proxy is relased
Docs
None
Description
Fixes a regression introduced #3 that caused the
promise to never settle which in turn prevented the endpoint from being closed.