SignalR
SignalR copied to clipboard
JS Client - Window unload event never unbind after stop connection
When connection starts, unload event bind on the window. But unload event never unbind after the connection stop.
It would remain many unload event on window.

Expected behavior
It should unbind unload event after the connection stop.
Actual behavior
It never unbind unload event after the connection stop.
Steps to reproduce
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<div>
<button id="start">Start Connection</button>
<button id="stop" disabled>Stop Connection</button>
</div>
@section scripts {
<script src="~/Scripts/jquery.signalR-2.4.0.min.js"></script>
<script src="~/signalr/hubs"></script>
<script>
$(function () {
$.connection.hub.logging = true;
$('#start').click(() => {
$.connection.hub.start();
$('#start').prop('disabled', true);
$('#stop').prop('disabled', false);
});
$('#stop').click(() => {
$.connection.hub.stop();
// $(window).unbind('unload');
$('#start').prop('disabled', false);
$('#stop').prop('disabled', true);
});
});
</script>
}
The demo has two button. One is start connection, the other is stop connection.
- Click
startbutton. - Click
stopbutton. - Go back to
1. - Repeat this for few time.
- Enter
$._data($(window)[0], 'events')on Console Tab in DevTools.
You will see the window has many unload event.
What's the issue this causes? Are you still having issues, or were you able to workaround them?
It may cause memory leak issue if the event is bound every time at the start of connection and never be unbound at the stop of connection.
Seeing this memory leak in our app as well. They end up growing quite quickly in an SPA with lots of starts
Does the commented-out $(window).unbind('unload'); work-around the issue? I know it's still a bad issue. Just curious.
Does the commented-out
$(window).unbind('unload');work-around the issue? I know it's still a bad issue. Just curious.
Yes!!
Unbind unload event self would be solve this issue.
We should be able to fix this, we'll look at it for the next patch release.
Any update there? passed 2 years