brefphp / bref

Serverless PHP on AWS Lambda
https://bref.sh
MIT License
3.11k stars 367 forks source link

Tmp file unzip throwing error #1254

Closed jusmark123 closed 1 year ago

jusmark123 commented 2 years ago

Hello suddenly started getting the below errors when my Lambda function is invoked. I am using the separateVendor directive. It seems the only way to fix it is to redeploy the app again.

2022-07-26T16:09:44 START RequestId: e4d438de-3ea5-587c-9174-63842d7a25d6 Version: $LATEST
2022-07-26T16:11:55 PHP Deprecated:  ZipArchive::open(): Using empty file as ZipArchive is deprecated in /opt/bref/breftoolbox.php on line 192
2022-07-26T16:11:55 Deprecated: ZipArchive::open(): Using empty file as ZipArchive is deprecated in /opt/bref/breftoolbox.php on line 192
2022-07-26T16:11:55 PHP Warning:  unlink(/tmp/vendor.zip): No such file or directory in /opt/bref/breftoolbox.php on line 72
2022-07-26T16:11:55 Warning: unlink(/tmp/vendor.zip): No such file or directory in /opt/bref/breftoolbox.php on line 72
2022-07-26T16:11:55 PHP Warning:  file_get_contents(/tmp/vendor/composer/autoload_static.php): Failed to open stream: No such file or directory in /opt/bref/breftoolbox.php on line 216
2022-07-26T16:11:55 Warning: file_get_contents(/tmp/vendor/composer/autoload_static.php): Failed to open stream: No such file or directory in /opt/bref/breftoolbox.php on line 216
2022-07-26T16:11:55 PHP Fatal error:  Uncaught TypeError: str_replace(): Argument #3 ($subject) must be of type array|string, bool given in /opt/bref/breftoolbox.php:216
2022-07-26T16:11:55 Stack trace:
2022-07-26T16:11:55 #0 /opt/bref/breftoolbox.php(216): str_replace()
2022-07-26T16:11:55 #1 /opt/bref/breftoolbox.php(74): Bref\ToolBox\BrefToolBox::updateComposerAutoloading()
2022-07-26T16:11:55 #2 /opt/bref/bootstrap.php(15): Bref\ToolBox\BrefToolBox::downloadAndConfigureVendor()
2022-07-26T16:11:55 #3 {main}
2022-07-26T16:11:55 thrown in /opt/bref/breftoolbox.php on line 216
2022-07-26T16:11:55 Fatal error: Uncaught TypeError: str_replace(): Argument #3 ($subject) must be of type array|string, bool given in /opt/bref/breftoolbox.php:216
2022-07-26T16:11:55 Stack trace:
2022-07-26T16:11:55 #0 /opt/bref/breftoolbox.php(216): str_replace()
2022-07-26T16:11:55 #1 /opt/bref/breftoolbox.php(74): Bref\ToolBox\BrefToolBox::updateComposerAutoloading()
2022-07-26T16:11:55 #2 /opt/bref/bootstrap.php(15): Bref\ToolBox\BrefToolBox::downloadAndConfigureVendor()
2022-07-26T16:11:55 #3 {main}
2022-07-26T16:11:55 thrown in /opt/bref/breftoolbox.php on line 216
2022-07-26T16:11:55 END RequestId: e4d438de-3ea5-587c-9174-63842d7a25d6
2022-07-26T16:11:55 REPORT RequestId: e4d438de-3ea5-587c-9174-63842d7a25d6  Duration: 130679.21 ms  Billed Duration: 130680 ms  Memory Size: 1024 MB    Max Memory Used: 32 MB
2022-07-26T16:11:55 RequestId: e4d438de-3ea5-587c-9174-63842d7a25d6 Error: Runtime exited with error: exit status 255
Runtime.ExitErro

My serverless is here for context, I've removed some account identifiers: Let me know if you need additional info.

provider:
    name: aws
    region: us-west-2
    runtime: provided.al2
    timeout: 900
    vpc:
        securityGroupIds:
            - security-group
        subnetIds:
            - subnet
            - subnet
            - subnet
            - subnet
plugins:
    - ./vendor/bref/bref

functions:
   app
        handler: handler.php
        description: "function description"
        events:
            - sqs:
               sqs-arn
                batchSize: 10
                maximumBatchingWindow: 10
                enabled: true
        layers:
            - ${bref:layer.php-81}

custom:
    bref:
        separateVendor: true
t-richard commented 2 years ago

The vendor directory is uploaded to the serverless deployment bucket.

Does the bucket still exist and does it contain the vendor archive?

jusmark123 commented 2 years ago

Yes, I checked the bucket is there and the .serverless folder with the vendor.zip is present. It's intermittent, so I'm not sure what is causing it. The function runs as expected sometimes, but then is suddenly starts failing with the above error.

2022-07-27T20:54:45 START RequestId: 07931007-5b63-4f86-a202-bc1da684a811 Version: $LATEST
2022-07-27T20:56:56 PHP Deprecated:  ZipArchive::open(): Using empty file as ZipArchive is deprecated in /opt/bref/breftoolbox.php on line 192
2022-07-27T20:56:56 Deprecated: ZipArchive::open(): Using empty file as ZipArchive is deprecated in /opt/bref/breftoolbox.php on line 192
2022-07-27T20:56:56 PHP Warning:  unlink(/tmp/vendor.zip): No such file or directory in /opt/bref/breftoolbox.php on line 72
2022-07-27T20:56:56 Warning: unlink(/tmp/vendor.zip): No such file or directory in /opt/bref/breftoolbox.php on line 72
2022-07-27T20:56:56 PHP Warning:  file_get_contents(/tmp/vendor/composer/autoload_static.php): Failed to open stream: No such file or directory in /opt/bref/breftoolbox.php on line 216
2022-07-27T20:56:56 Warning: file_get_contents(/tmp/vendor/composer/autoload_static.php): Failed to open stream: No such file or directory in /opt/bref/breftoolbox.php on line 216
2022-07-27T20:56:56 PHP Fatal error:  Uncaught TypeError: str_replace(): Argument #3 ($subject) must be of type array|string, bool given in /opt/bref/breftoolbox.php:216
2022-07-27T20:56:56 Stack trace:
2022-07-27T20:56:56 #0 /opt/bref/breftoolbox.php(216): str_replace()
2022-07-27T20:56:56 #1 /opt/bref/breftoolbox.php(74): Bref\ToolBox\BrefToolBox::updateComposerAutoloading()
2022-07-27T20:56:56 #2 /opt/bref/bootstrap.php(15): Bref\ToolBox\BrefToolBox::downloadAndConfigureVendor()
2022-07-27T20:56:56 #3 {main}
2022-07-27T20:56:56 thrown in /opt/bref/breftoolbox.php on line 216
2022-07-27T20:56:56 Fatal error: Uncaught TypeError: str_replace(): Argument #3 ($subject) must be of type array|string, bool given in /opt/bref/breftoolbox.php:216
2022-07-27T20:56:56 Stack trace:
2022-07-27T20:56:56 #0 /opt/bref/breftoolbox.php(216): str_replace()
2022-07-27T20:56:56 #1 /opt/bref/breftoolbox.php(74): Bref\ToolBox\BrefToolBox::updateComposerAutoloading()
2022-07-27T20:56:56 #2 /opt/bref/bootstrap.php(15): Bref\ToolBox\BrefToolBox::downloadAndConfigureVendor()
2022-07-27T20:56:56 #3 {main}
2022-07-27T20:56:56 thrown in /opt/bref/breftoolbox.php on line 216
2022-07-27T20:56:56 END RequestId: 07931007-5b63-4f86-a202-bc1da684a811
2022-07-27T20:56:56 REPORT RequestId: 07931007-5b63-4f86-a202-bc1da684a811  Duration: 131239.25 ms  Billed Duration: 131240 ms  Memory Size: 1024 MB    Max Memory Used: 32 MB
2022-07-27T20:56:56 RequestId: 07931007-5b63-4f86-a202-bc1da684a811 Error: Runtime exited with error: exit status 255
Runtime.ExitError
jusmark123 commented 2 years ago

Also, getting this error as well.

ERROR: Uncaught Exception: Error while calling the Lambda runtime API: The requested URL returned error: 403 in /tmp/vendor/bref/bref/src/Runtime/LambdaRuntime.php:291
Stack trace:
#0 /tmp/vendor/bref/bref/src/Runtime/LambdaRuntime.php(223): Bref\Runtime\LambdaRuntime->postJson()
#1 /tmp/vendor/bref/bref/src/Runtime/LambdaRuntime.php(95): Bref\Runtime\LambdaRuntime->signalFailure()
#2 /opt/bref/bootstrap.php(43): Bref\Runtime\LambdaRuntime->processNextEvent()
#3 {main}
thrown 
in /tmp/vendor/bref/bref/src/Runtime/LambdaRuntime.php:291
END RequestId: 10101995-fa97-469c-87f5-e1c6914f42d4
REPORT RequestId: 10101995-fa97-469c-87f5-e1c6914f42d4  Duration: 1090.12 ms    Billed Duration: 2383 ms    Memory Size: 1024 MB    Max Memory Used: 172 MB Init Duration: 1292.37 ms   
RequestId: 10101995-fa97-469c-87f5-e1c6914f42d4 Error: Runtime exited with error: exit status 255
Runtime.ExitError

I have the following permissions set for the lambda role. Not sure if this is related:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor1",
            "Effect": "Allow",
            "Action": [
                "sqs:DeleteMessage",
                "sts:AssumeRole",
                "logs:CreateLogStream",
                "lambda:InvokeFunctionUrl",
                "lambda:InvokeFunction",
                "sqs:ReceiveMessage",
                "sqs:SendMessage",
                "lambda:InvokeAsync",
                "sqs:GetQueueAttributes",
                "logs:CreateLogGroup",
                "logs:PutLogEvents"
            ],
            "Resource": "*"
        }
    ]
}
t-richard commented 2 years ago

The serverless framework deletes older deployment artifacts.

Maybe it's related? See https://www.serverless.com/framework/docs/providers/aws/guide/serverless.yml#deployment-bucket

afu-dev commented 2 years ago

The serverless framework deletes older deployment artifacts.

Maybe it's related? See https://www.serverless.com/framework/docs/providers/aws/guide/serverless.yml#deployment-bucket

I tested it with a maxPreviousDeploymentArtifacts at 2 and the vendors directory is not deleted (nor the zip file).

I see that the breftoolbox.php file is using the getenv function and, from the error log, it seems like the function is returning an empty string (or a casted null?). Can it be a problem with the fact that getenv is not thread-safe?

@jusmark123 do you have a way to reproduce the problem from a new project?

mnapoli commented 2 years ago

I see that the breftoolbox.php file is using the getenv function and, from the error log, it seems like the function is returning an empty string (or a casted null?). Can it be a problem with the fact that getenv is not thread-safe?

I don't know if that's the case, but IIRC we use that function everywhere.

afu-dev commented 2 years ago

Ref of the latest pull request talking about this: #662

mnapoli commented 2 years ago

👍 sorry I meant I'm not sure if that is the source of the problem. So far we should do anything multi-threaded in Bref and apps running with Bref, but it may also be some very weird edge case ^^

afu-dev commented 2 years ago

Yes, it wasn't to prove my point, I'm not sure if that is the source of the problem either 😉

I'll try to reproduce the bug later or wait for a return.

jusmark123 commented 2 years ago

Sorry this project has a quick turnaround requirement so I decided to build my own custom runtime.

mnapoli commented 1 year ago

Closing because the issue isn't active anymore.