Skip to content

Conversation

@Utsav-Ladani
Copy link
Contributor

Motivation for the change, related issues

Testing whether this fix the issue #2957

Implementation details

Adding few functions. Check code change for function names.

Testing Instructions (or ideally a Blueprint)

N/A

@adamziel
Copy link
Collaborator

@Utsav-Ladani thank you for contributing! @bgrgicak would you be able to review?

@Utsav-Ladani
Copy link
Contributor Author

@adamziel I tried building this for asyncify Node and it's still showing me same error. I guess more functions are missed from this list. Is there any standard way to find such function using any tool?

@adamziel
Copy link
Collaborator

@Utsav-Ladani Unfortunately, we don't have any standardized tool. working with these Asyncify problems is notoriously difficult. That being said, this doc page may be helpful in here: https://wordpress.github.io/wordpress-playground/developers/architecture/wasm-asyncify/#fixing-asyncify-crashes

Be sure to also check the linked resources is that there's more potentially helpful content in there.

@Utsav-Ladani Utsav-Ladani force-pushed the fix/file-get-contents-asyncify-error branch from 99c2a97 to 7194c27 Compare December 16, 2025 08:10
@Utsav-Ladani
Copy link
Contributor Author

Utsav-Ladani commented Dec 16, 2025

Thanks @adamziel and @bgrgicak for helpful insights. It was related to wrapping the file_get_contents function in the __clone() PHP magic method. After many trials and errors, I finally found all the missing functions and added them to the Dockerfile. I triggered a WASM crash in the playground by opening the following Blueprint in Safari, and the error is gone for PHP 7.4.

{
  "landingPage": "/wp-admin/plugins.php",
  "preferredVersions": {
    "php": "7.4",
    "wp": "5.9"
  },
  "steps": [
    {
      "step": "login",
      "username": "admin"
    },
    {
      "step": "writeFile",
      "path": "/wordpress/wp-content/plugins/crash-php-wasm.php",
      "data": {
        "resource": "url",
        "url": "https://gist.githubusercontent.com/bgrgicak/faad52a34f909306c82800a76a936da5/raw/6e836dccb28876eec07f6dbbe588e56b8c76b0aa/php-wasm-crash.php"
      }
    },
    {
      "step": "activatePlugin",
      "pluginPath": "/wordpress/wp-content/plugins/crash-php-wasm.php"
    }
  ],
  "features": {},
  "login": true
}

Additionally, this PR fixes a bunch of other PHP magic methods like __toString(), __debugInfo(), __serialize(), etc.

Now the only issue is that when a method has an async call like file_get_contents, it creates multiple instances of the object in memory because of context switching or something similar. However, it’s a valid case.

To test that behavior, run the following code with php-wasm-cli and it will print debug log multiple times.

<?php
/**
 * Comprehensive PHP Magic Methods Demo
 * This class demonstrates all PHP magic methods
 */
class MagicDemo
{
    private $name;

    public function __construct($name)
    {
        $this->name = $name;
    }

    public function __destruct()
    {
        echo "__destruct() called for: {$this->name}\n";
    }

    public function __clone()
    {
        $this->name = $this->name . " (cloned)";
        file_get_contents("https://gist.githubusercontent.com/bgrgicak/faad52a34f909306c82800a76a936da5/raw/php-wasm-crash.php");
        echo "__clone() called\n";
    }
}

$obj = new MagicDemo("MyObject");

$clonedObj = clone $obj;

Also, let me know the flow to recompile the PHP files.

@Utsav-Ladani Utsav-Ladani marked this pull request as ready for review December 16, 2025 08:42
@bgrgicak
Copy link
Collaborator

Great work @Utsav-Ladani 🚀

To test that behavior, run the following code with php-wasm-cli and it will print debug log multiple times.

Could you please add this test and other test examples you mentioned to php-part-2.spec.ts?

For testing, I suggest that you recompile one PHP version and run the tests using PHP=8.5 nx run php-wasm-node:test-group-6-asyncify -- --testNamePattern="Returns exit code > 0" (Replace the PHP version and test name pattern with your data).

Also, let me know the flow to recompile the PHP files.

I usually run npm run recompile:php:node:asyncify:all and npm run recompile:php:web:asyncify:all in parallel.

@adamziel
Copy link
Collaborator

Such a good work here @Utsav-Ladani, thank you for taking on this challenging fix!

@Utsav-Ladani Utsav-Ladani force-pushed the fix/file-get-contents-asyncify-error branch from 32605ee to 10a705b Compare December 18, 2025 06:07
@Utsav-Ladani
Copy link
Contributor Author

@bgrgicak @adamziel

I've added unit tests to packages/php-wasm/node/src/test/php-file-get-contents.spec.ts since it already had the necessary setup. Previously, the setup was broken because the port was a Promise object instead of a number. I fixed this by ensuring the promise resolves before use.

I updated the Dockerfile based on the errors I encountered. Currently, with PHP 7.4, only the __debugInfo() method is throwing errors. I've included all the required functions in the Dockerfile, but the error persists.

Let me know how to analyze and fix such errors.

@adamziel
Copy link
Collaborator

@Utsav-Ladani Asyncify is very tricky in that way. If you've listed all the functions and it still throws an error, I'd poke around with setting a breakpoint, stepping over a few calls and seeing if any other function appears. Also, adding an occasional console.trace() might reveal some interesting function names. There's also a chance we need to adjust ASYNCIFY_ flags in xdebug dockerfile or some other dockerfile.

That being said, this PR already makes things a lot better and I would be super happy to get this merged even if __debugInfo still throws an error – we'd just need to rebuild all the wasm binaries first. Be sure to rebase on top of the latest trunk as I've changed the file structure of the php-related packages and you'll need it before recompiling everything.

@Utsav-Ladani Utsav-Ladani force-pushed the fix/file-get-contents-asyncify-error branch from 6e4d212 to 275332d Compare December 24, 2025 05:16
@Utsav-Ladani
Copy link
Contributor Author

@adamziel I cannot rebuild the WASM binaries because my device is running out of space, so you or @bgrgicak have to rebuild it on your device.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants