phpiisflow-framework

File downloads are randomly failing in IIS with PHP


We have a website that streams files to our customers. Now, we started having files bigger than 300Mb and for no apparent reasons, the downloads are randomly failing. We get a "the download was interrupted" error.

We logged in to the server and figured out that the PHP processes were sometimes closing when a download would start but there's nothing significant either in the event viewer or in the logs.

Here's the code that delivers the downloads:

public function downloadAction($asset, $csrfToken) {
        if ($this->securityContext->isCsrfProtectionTokenValid($csrfToken) === FALSE) {
            throw new \TYPO3\Flow\Security\Exception\AccessDeniedException();
        }

        // This is a : \TYPO3\Flow\Resource\Resource
        $resource = $asset->getResource();
        $stream = $resource->getStream();

        if (FALSE === $stream) {
            $this->throwStatus(500);
        }

        try {
            $streamMetadata = stream_get_meta_data($stream);
            $modified = filemtime($streamMetadata['uri']);
            $filename = $asset->getResource()->getFilename();

            $this->response->setStatus(200);
            $this->response->setHeader('Content-Type', 'application/octet-stream');
            $this->response->setHeader('Content-Disposition', 'attachment; filename="' . $filename . '"');
            $this->response->setHeader('Last-Modified', gmdate('r', $modified));
            $this->response->setHeader('Content-Length', $asset->getResource()->getFileSize());
            $this->response->setHeader('Content-transfer-encoding', 'binary');
            $this->response->sendHeaders();

            @set_time_limit(86400);

            $fileBuffer = 8192;
            while (!feof($stream)) {
                print(fread($stream, $fileBuffer));
                ob_flush();
                flush();
            }
        } finally {
            fclose($stream);
        }

        return '';
    }

Here's our setup:

What I tried:

I tried to increase the memory_limit to 1024Mb, to increase the timeout, to increase the script execution time and we didn't get any benefits from increasing those.

From the event viewer, we are getting a lot of errors when the downloads fail:

Faulting application name: w3wp.exe, version: 7.5.7601.17514, time stamp: 0x4ce7afa2
Faulting module name: unknown, version: 0.0.0.0, time stamp: 0x00000000
Exception code: 0xc0000005
Fault offset: 0x0000000000d94b98
Faulting process id: 0x26a0
Faulting application start time: 0x01d1c3157fec8818
Faulting application path: c:\windows\system32\inetsrv\w3wp.exe
Faulting module path: unknown
Report Id: 9ea4b22a-2f09-11e6-8366-00155d016700

Faulting application name: w3wp.exe, version: 7.5.7601.17514, time stamp: 0x4ce7afa2
Faulting module name: nativerd.dll, version: 7.5.7601.17855, time stamp: 0x4fc85321
Exception code: 0xc0000005
Fault offset: 0x000000000000f4d3
Faulting process id: 0x2170
Faulting application start time: 0x01d1c310490342e2
Faulting application path: c:\windows\system32\inetsrv\w3wp.exe
Faulting module path: c:\windows\system32\inetsrv\nativerd.dll
Report Id: 9ea48b1a-2f09-11e6-8366-00155d016700

Faulting application name: w3wp.exe, version: 7.5.7601.17514, time stamp: 0x4ce7afa2
Faulting module name: iisfcgi.dll, version: 7.5.7601.17514, time stamp: 0x4ce7c6cb
Exception code: 0xc0000005
Fault offset: 0x0000000000007a47
Faulting process id: 0x28b8
Faulting application start time: 0x01d1c3016d933dec
Faulting application path: c:\windows\system32\inetsrv\w3wp.exe
Faulting module path: C:\Windows\System32\inetsrv\iisfcgi.dll
Report Id: 82a04d74-2f03-11e6-8366-00155d016700

Edit 1

It looks like modifying the buffer an putting it at 12000 works, but not at 11999. What kind of sorcery is this?


Solution

  • Apparently, putting the $fileBuffer at 12000 works but not at 11999... But why?