SignalR icon indicating copy to clipboard operation
SignalR copied to clipboard

JS Client - Window unload event never unbind after stop connection

Open peterhpchen opened this issue 7 years ago • 7 comments

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.

image

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.

  1. Click start button.
  2. Click stop button.
  3. Go back to 1.
  4. Repeat this for few time.
  5. Enter $._data($(window)[0], 'events') on Console Tab in DevTools.

You will see the window has many unload event.

peterhpchen avatar Dec 04 '18 00:12 peterhpchen

What's the issue this causes? Are you still having issues, or were you able to workaround them?

muratg avatar Jan 18 '19 19:01 muratg

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.

peterhpchen avatar Mar 25 '19 15:03 peterhpchen

Seeing this memory leak in our app as well. They end up growing quite quickly in an SPA with lots of starts

lawrencecushman avatar Apr 03 '19 18:04 lawrencecushman

Does the commented-out $(window).unbind('unload'); work-around the issue? I know it's still a bad issue. Just curious.

halter73 avatar Apr 09 '19 21:04 halter73

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.

peterhpchen avatar Apr 10 '19 00:04 peterhpchen

We should be able to fix this, we'll look at it for the next patch release.

analogrelay avatar Sep 25 '19 17:09 analogrelay

Any update there? passed 2 years

alexmurug avatar Aug 03 '21 15:08 alexmurug