Problem/Motivation

The fail result is at: https://www.drupal.org/pift-ci-job/339437

It begins:

Aggregator.Drupal\aggregator\Tests\AggregatorRenderingTest
✗	
ile_save_htaccess
exception: [Warning] Line 364 of core/includes/file.inc:
file_put_contents(temporary://.htaccess): failed to open stream: "Drupal\Core\StreamWrapper\TemporaryStream::stream_open" call failedfile_put_contents('temporary://.htaccess', '# Deny all requests from Apache 2.4+.

  Require all denied

Similar fails for HEAD:
https://www.drupal.org/pift-ci-job/529116
https://www.drupal.org/pift-ci-job/447899
https://www.drupal.org/pift-ci-job/375643
https://www.drupal.org/pift-ci-job/294179

Proposed resolution

The reason this happens is that during drupal install for a test the temporary directory is falling back to the system temporary directory. Therefore multiple processes are trying to write temporary://.htaccess at the same time.

Remaining tasks

User interface changes

None

API changes

None

Data model changes

None

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

martin107 created an issue. See original summary.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

xjm’s picture

Priority: Normal » Critical

Thanks @martin107 for the report!

The fail result is at: https://www.drupal.org/pift-ci-job/339437

Aggregator.Drupal\aggregator\Tests\AggregatorRenderingTest
✗	
ile_save_htaccess
exception: [Warning] Line 364 of core/includes/file.inc:
file_put_contents(temporary://.htaccess): failed to open stream: "Drupal\Core\StreamWrapper\TemporaryStream::stream_open" call failedfile_put_contents('temporary://.htaccess', '# Deny all requests from Apache 2.4+.

  Require all denied


# Deny all requests from Apache 2.0-2.2.

  Deny from all

# Turn off all options we don't need.
Options -Indexes -ExecCGI -Includes -MultiViews

# Set the catch-all handler to prevent scripts from being executed.
SetHandler Drupal_Security_Do_Not_Remove_See_SA_2006_006

  # Override the handler again if we're run later in the evaluation list.
  SetHandler Drupal_Security_Do_Not_Remove_See_SA_2013_003


# If we know how to do it safely, disable the PHP engine entirely.

  php_flag engine off
') (Line: 364)
file_save_htaccess('temporary://', 1) (Line: 317)
file_ensure_htaccess() (Line: 1044)
install_base_system(Array) (Line: 658)
install_run_task(Array, Array) (Line: 536)
install_run_tasks(Array) (Line: 115)
install_drupal(Object, Array) (Line: 577)
Drupal\simpletest\WebTestBase->doInstall() (Line: 549)
Drupal\simpletest\WebTestBase->setUp() (Line: 33)
Drupal\aggregator\Tests\AggregatorTestBase->setUp() (Line: 23)
Drupal\aggregator\Tests\AggregatorRenderingTest->setUp() (Line: 1046)
Drupal\simpletest\TestBase->run(Array) (Line: 723)
simpletest_script_run_one_test('25', 'Drupal\aggregator\Tests\AggregatorRenderingTest') (Line: 59)

This also happened in HEAD recently for a different test: https://www.drupal.org/pift-ci-job/529116 In that case there was also a know failure due to #2806697: Random fail for AlreadyInstalledException.

xjm’s picture

Title: Random Test Failure AggregatorRenderingTest » Random Test Failure with "failed to open stream"
Related issues: +#2806697: Random fail for AlreadyInstalledException

I also wonder if this might be the same bug as #2806697: Random fail for AlreadyInstalledException, just manifesting differently.

xjm’s picture

xjm’s picture

Issue summary: View changes
xjm’s picture

xjm’s picture

Issue tags: +Triaged D8 critical

@catch, @alexpott, @effulgentsia, @Cottser, and I agreed that this is critical as a random test failure under normal circumstances in HEAD.

alexpott’s picture

Status: Active » Needs review
FileSize
10.42 KB

So this occurs because during the installer the temporary directory is not yet set to the test specific one. We need to move the setting of this to be inside the installer. Unfortunately there is no other way.

Here's another occurrence https://www.drupal.org/pift-ci-job/571518.

alexpott’s picture

Title: Random Test Failure with "failed to open stream" » Random Test Failure with "failed to open stream" for temporary://.htaccess
Issue summary: View changes

Status: Needs review » Needs work

The last submitted patch, 9: 2754217-9.patch, failed testing.

alexpott’s picture

oops should have read my own added documentation - the temp directory expected by the test system in not inside the public files directory but it is just up one level.

For example:

    // Prepare filesystem directory paths.
    $this->publicFilesDirectory = $this->siteDirectory . '/files';
    $this->privateFilesDirectory = $this->siteDirectory . '/private';
    $this->tempFilesDirectory = $this->siteDirectory . '/temp';
    $this->translationFilesDirectory = $this->siteDirectory . '/translations';
alexpott’s picture

Status: Needs work » Needs review
Mixologic’s picture

I just found some older evidence that this has been around a long time : https://www.drupal.org/node/2656720#comment-10872502

Berdir’s picture

Status: Needs review » Reviewed & tested by the community

This looks good to me, explanation makes sense. @dawehner agrees.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

In discussing with @xjm I realised that we can test that we're not writing an .htaccess to the system temp directory and that a .htaccess is present in the test specific directory.

alexpott’s picture

Here's a patch with tests for both Simpletest and BrowserTestBase. Also on @xjm's suggestion I've improved the comments in \Drupal\Core\Test\FunctionalTestSetupTrait to detail where everything actually occurs.

To test this locally:

  • Ensure you do not have a .htaccess in your system temp directory - on my system that's /tmp
  • Run a test - for example: \Drupal\FunctionalTests\BrowserTestBaseTest
  • Without the patch - check that the .htaccess exists in the system temp
  • Apply patch and remove the /tmp/.htaccess
  • Run a test - for example: \Drupal\FunctionalTests\BrowserTestBaseTest
  • /tmp/.htaccess should not exist

Note that the parent site might create an .htaccess in the system temp folder - this is why the above steps should be done from the CLI and we don't assert that a .htaccess does in exist in that location in the new tests.

As this patch does not apply to 8.2.x we'll need to backport once it lands.

The last submitted patch, 17: 2754217-17.test-only.patch, failed testing.

dawehner’s picture

In discussing with @xjm I realised that we can test that we're not writing an .htaccess to the system temp directory

Mh, I don't really see this as part of the added test code ...

alexpott’s picture

re #19 -

Note that the parent site might create an .htaccess in the system temp folder - this is why the above steps should be done from the CLI and we don't assert that a .htaccess does in exist in that location in the new tests.
dawehner’s picture

Status: Needs review » Reviewed & tested by the community

Fair

  • xjm committed 3af6a3e on 8.3.x
    Issue #2754217 by alexpott, xjm, dawehner, martin107: Random Test...
xjm’s picture

Status: Reviewed & tested by the community » Patch (to be ported)

Committed to 8.3.x, thanks!

Setting PTBP for 8.2.x.

alexpott’s picture

Version: 8.3.x-dev » 8.2.x-dev
Status: Patch (to be ported) » Needs review
FileSize
8.06 KB

Here's a patch for 8.2.x - there are a number of differences because of improvements made to the how WebTestBase and BrowserTestBase share code.

catch’s picture

Status: Needs review » Reviewed & tested by the community

Backport looks good to me, just a bit more change necessary, moving to RTBC.

catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to 8.2.x, thanks!

  • catch committed add4f52 on 8.2.x
    Issue #2754217 by alexpott, xjm, martin107, dawehner: Random Test...

  • xjm committed 3af6a3e on 8.4.x
    Issue #2754217 by alexpott, xjm, dawehner, martin107: Random Test...

  • xjm committed 3af6a3e on 8.4.x
    Issue #2754217 by alexpott, xjm, dawehner, martin107: Random Test...
xjm’s picture

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.