Skip to content

Could the CurrentApplication set method be concurrently unsafe? #241

@godruoyi

Description

@godruoyi

When a request comes in, we use CurrentApplication::set to set the current container. But is this concurrency safe?

Worker.php#L83

public function handle(Request $request, RequestContext $context): void
{
    CurrentApplication::set($sandbox = clone $this->app);

    try {
        // process...
    } catch (Throwable $e) {
    } finally {
        CurrentApplication::set($this->app);
    }
}

For example:

                                                worker1

                    request1                                                    request2
                       |                                                            |
                       |                                                            |
                       V                                                            |
CurrentApplication::set($sandbox1 = clone $this->app);                              |
                       |                                                            |
                       |                                                            |
                       |                                                            V
                       |                                     CurrentApplication::set($sandbox2 = clone $this->app);
                       |
                       V
                    app()->make(x) // ? use sandbox1 container or sandbox2 container.
                       |                                                            |
                       |                                                            |
                       V                                                            V
                      end                                                          end

When I use Container::getInstance in request 1, I'm probably getting the sandbox2 in request 2 set.

So, Is this allowed?

  • Octane Version: ^0.3.2
  • Laravel Version: ^8.12
  • PHP Version: 8.0.3
  • Server: Swoole

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions