What could be the reason for Firefox abruptly terminating inactive XMLHttpRequest connections?

When working with a JavaScript class, an XMLHttpRequest is used to connect to the server. The server sends data slowly, and this process functions well in Chromium. However, Firefox tends to close the connection randomly after a certain amount of time (typically between approximately 4 seconds and 70 seconds).

What could be causing Firefox to close the connection, and how can this issue be prevented?

Here is a simplified version of the JS code being used:

var options = {};
options['header']=
{ 'Cache-Control':'no-cache, max-age=0', 
'Content-type': 'application/octet-stream',
'Content-Disposition': 'inline'
};

// Get request information
this.http = new XMLHttpRequest();
this.http.onreadystatechange = _streamingResponse.bind(this);
this.http.open('post', url, true);
for (var i in options['header'])
{
this.http.setRequestHeader(i, options['header'][i]);
}
this.http.send('');

For the PHP component, something like this may occur:

sleep(200); //wait for a long time, resulting in Firefox closing the socket.

If the server sends data every few seconds (less than 5 seconds), the connection remains active indefinitely. However, if no data is transmitted, Firefox closes the connection.

The connection closure details are as follows: - readyState = 4 - status = 0

The server appears to function correctly since it works as expected in Chromium.

Complete test code:

test.html

<html>
<header>
</header>
<body>
</body>

<script type="application/javascript">

function log( msg )
{
document.body.appendChild(document.createElement('div').appendChild(document.createTextNode(msg)));
document.body.appendChild(document.createElement('br'));
}

function request(url)
{
function _streamingResponse()
{
if (4==this.http.readyState)
{
log('Done: ' + this.http.status);
}
else if (3==this.http.readyState)
{
var text = this.http.response.substr(this.lastRequestPos);
this.lastRequestPos = this.http.response.length;
log('Update: ' + text);
}
}

var options = {};
options['header']=
{ 'Cache-Control':'no-cache, max-age=0', 
'Content-type': 'application/octet-stream',
'Content-Disposition': 'inline'
};

this.lastRequestPos=0;

// Get request information
this.http = new XMLHttpRequest();
this.http.onreadystatechange = _streamingResponse.bind(this);
this.http.open('post', url, true);
for (var i in options['header'])
{
this.http.setRequestHeader(i, options['header'][i]);
}
this.http.send('');
log('Request sent!');
}

req = new request('./test.php');
</script>
</html>

test.php

<?php

$timer = 60;

ignore_user_abort(true);
set_time_limit(0);

// Turn off output buffering and compression
ini_set('output_buffering', 'off');
ini_set('zlib.output_compression', false);
ini_set('implicit_flush', true);
ob_implicit_flush(true);
while (ob_get_level() > 0) {
$level = ob_get_level();
ob_end_clean();
if (ob_get_level() == $level) break;
}
if (function_exists('apache_setenv')) {
apache_setenv('no-gzip', '1');
apache_setenv('dont-vary', '1');
}

// Set header for streaming
header('Content-type: application/octet-stream');
flush();

// Send information
sleep($timer);
echo '<yes></yes>';
flush();

?>

Additional note: Firefox 43.0.03, Chromium 47.0.2526

EDITED:

Setting a callback for timeout does not seem to trigger. I have concluded that it is not necessarily a timeout issue.

this.http.timeout = 2000;
this.http.ontimeout = _streamingTimeout.bind(this);

Answer №1

Upon further investigation, I stumbled upon a bug in Mozilla that appears to be the cause of this issue. It is expected to be resolved in version 45, but for now, we'll have to deal with it.

Interestingly, while the bug seems to be related to IPv6, I encountered the same problem using 127.0.0.1. However, the issue appears to be resolved with Firefox Developer Edition (V 45).

Why is Firefox terminating the connection?

This should not be happening. More information can be found at: https://bugzilla.mozilla.org/show_bug.cgi?id=1240319

How can this be fixed?

Currently, the only solution seems to be sending data every 3-4 seconds to keep the connection open. Other than that, there doesn't seem to be a clear resolution.

Answer №2

This reminds me a lot of the concept of garbage collection in programming.

It appears that you have experimented with setting a timeout, but I'm a bit confused by your statement: Even when a callback is set for the timeout, it does not seem to trigger.

Make sure to carefully adhere to these guidelines:

4.2 Garbage collection

An XMLHttpRequest object should not be eligible for garbage collection if its state is open with the send() flag activated, headers received, or loading, and it has event listeners registered for any of the following types: readystatechange, progress, abort, error, load, timeout, and loadend.

If an XMLHttpRequest object is garbage collected while its connection is still active, the browser must terminate the request.

xhr.spec.whatwg.org

Examine the states, send() flag, and event listener configurations when no data is being transmitted.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Sails.js encounters issues initializing Passport.js when SSL is implemented

Currently utilizing Sails.js (v0.9.4) and passport(local strategy) with no issues. The need arises to configure Sails.js behind SSL. Therefore, the setup includes: (Port 8080 selected for SSL). Upon starting Sails, it responds correctly, except for those ...

Sending data from child components to parent components in Angular

I'm facing an issue with retrieving data from a child array named store within user data returned by an API. When trying to access this child array in my view, it keeps returning undefined. Code export class TokoPage implements OnInit { store= nu ...

Changing data structures within JSON objects using nested arrays

Having trouble formatting data from my web form using "react-hook-form" for my api. Currently, I'm inputting the [] date field which is not ideal, but it's a temporary workaround to move forward. The data needs to be sent over in the following ...

Sequelize is incorrectly pointing to an invalid foreign key

There are two tables in my database, namely Transaction and Transaction Details. The recordId serves as a foreign key in the Transaction Details table. My goal is to query a specific Transaction based on its recordId and include all associated Transaction ...

Discover the correct way to locate the "_id" field, not the "id" field, within an array when using Javascript and the MEAN stack

I am working on an Angular application that connects to MongoDB. I have a data array named books, which stores information retrieved from the database. The structure of each entry in the array is as follows: {_id: "5b768f4519d48c34e466411f", name: "test", ...

The metro bundler is facing an unexpected glitch and is stuck in the terminal, failing to load

Recently, I've been using explo-cli to work on a react native project. Everything was running smoothly until today when I encountered an error stating that it couldn't find module './iter-step'. Before this, there was also an issue with ...

Creating HTML inputs dynamically using jQuery in an ASP.NET application

I have written this JavaScript code to dynamically generate and add HTML elements within my asp.net webform: var i = 0; function addFields() { i++; var quantity = '<td class="auto-style2"><input type="text" size="3" name= ...

Exploring the JSON structure

Struggling to come up with a solution as I am limited by certain restrictions. I need to create a mobile navigation system using a JSON object provided by the proprietary server. The only allowed framework is jQuery 1.12.4, no other frameworks or updated v ...

Implementing Multiple HTML Files Loading in QUnit

Currently, I am utilizing QUnit for unit testing JavaScript and jQuery. The structure of my HTML document is as follows: <!DOCTYPE html> <html> <head> <title>QUnit Test Suite</title> <script src="../lib/jquery.js">< ...

What is the best way to show nested labels on separate lines simultaneously?

Despite attempting to utilize the map method as suggested in my research, it appears that it may not be suitable for the structure I am working with: const stepLabels = [ { labels1: ['First One'] }, { labels2: ['Second One', 's ...

Is it possible for the server to initiate communication with the client

Every time I try to find a solution, Comet always comes up. But it seems like overkill for what I need - just around 100 users at most, with maybe 10 online simultaneously. Is there a simpler and more suitable option for my needs, such as pushing data to ...

Nest.js: initializing properties from a superclass in a controller

I have a question about unit testing controllers in the Nest.js framework. My issue is that the property from a superclass is not initialized in the controller class when creating a test module. Here is an example of the code I am referring to: export cl ...

Rules for validating string and numeric combinations in Vuetify are essential for ensuring accurate

Looking for guidance on implementing Vuetify validation to enforce rules (using :rules tag on a v-text-field) in the format of AB-12345678 (starting with two letters followed by a hyphen and then an 8-digit number). I'm having difficulty achieving thi ...

Using a 2D png image as a texture around a 3D sphere in Three.js results in a dark appearance

As I explore THREE.js to showcase a 3D rotating earth on the web, my goal is to also have an image surrounding the spinning globe. Despite trying various methods, none seemed to work out for me. The image loader option I attempted failed to display anythi ...

Discovering the method to access a getter from a different JavaScript file

In a laravel + vuejs project, I am trying to access my currentUser role in my Acces.js file using store.getters.currentUser but encountering an error: Error in render: "TypeError: Cannot read property 'getters' of undefined I have tried mu ...

Implementing the Infinite Scroll feature with 'react-infinite-scroll-component' on local JSON data: A step-by-step guide

Currently experimenting with frontEnd development, I have incorporated the 'react-infinite-scroll-component' as a dependency. My goal is to apply this effect to my local Json data without relying on any API calls at this stage. I'm encounter ...

Discrepancies in rounding calculations between three.js on Chrome and Firefox

In my three JS scene, I have a plane positioned and rotated in a certain way. When I send a raycast towards this plane and project an object onto it, everything works perfectly fine in chromium but gets turned upside down in firefox. This discrepancy is c ...

Customizing an image with personalized text and then sending it to a server using a combination of javascript and

Looking for a way to add text to an image saved on the server, then save the edited image back to the server. I've heard using canvas is the best approach, but struggling to find the specific solution I need. ...

Issues arising from TypeScript error regarding the absence of a property on an object

Having a STEPS_CONFIG object that contains various steps with different properties, including defaultValues, I encountered an issue while trying to access the defaultValues property from the currentStep object in TypeScript. The error message indicated tha ...

React snap scrolling feature is not working as intended

I've been attempting to implement the snap scroll feature in my react app, but I'm facing issues with it. The regular CSS method doesn't seem to work well in Chrome, as discussed in this Scroll Snap Skips Section on Smaller Screen - Chrome t ...