"Call to a member function eof() on null" can be returned in certain circumstances

Created on 18 August 2021, over 3 years ago
Updated 15 January 2022, almost 3 years ago

Problem/Motivation

We're not 100% sure why this happens, but every so often when our site is requesting data from another system through a remote stream wrapper over HTTP, the site will error out with the error Error: Call to a member function eof() on null in Drupal\remote_stream_wrapper\StreamWrapper\HttpStreamWrapper->stream_eof() (line 116).

Looking through the code, I can see only two situations that would cause $this->stream to be null for an open stream:

  1. Only stat() was called, so HttpStreamWrapper::request() did not populate the response because it only did a HEAD request;
    OR
  2. The response body was missing (503? 504? Connection interrupted?).

I think situation 2 is the more likely case since I can't see any way for the stream resource to be open for an fstat() call.

Steps to reproduce

Not 100% sure on repro, but I think the situation is:

  1. Open an HTTP-stream to a remote server that closes the connection instead of sending back a body in response to a GET request.
  2. Attempt to read from the HTTP stream, calling stream_eof() at the top of the loop:
    $wrapper = new \Drupal\remote_stream_wrapper\StreamWrapper\HttpStreamWrapper();
    $wrapper->setUri('http://path/to/badserver');
    
    if ($wrapper->stream_open()) {
      while (!$wrapper->stream_eof()) {
        print $wrapper->stream_read() . "\n";
      }
    }
    

Proposed resolution

If the response body is empty in HttpStreamWrapper::request(), populate the stream with an empty array-based stream.

Remaining tasks

User interface changes

API changes

Data model changes

πŸ› Bug report
Status

Needs review

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States GuyPaddock

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024