Skip to content

startSession immediately triggers deliverSessions #666

@alcohol

Description

@alcohol

Perhaps the behaviour I am about to describe is intentional, in which case this would be more of a feature (change) request than a bug report.

We currently use a Symfony EventSubscriber to automatically track sessions; see code:

<?php declare(strict_types=1);

namespace Vendor\Events\EventSubscriber;

use Bugsnag\Client;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\Event\TerminateEvent;
use Symfony\Component\HttpKernel\KernelEvents;

class BugsnagSubscriber implements EventSubscriberInterface
{
    public function __construct(
        #[Autowire(value: '%kernel.environment%')]
        private readonly string $kernelEnvironment,
        private readonly Client $bugsnag
    ) {
    }

    public function onKernelRequest(RequestEvent $event): void
    {
        if ('docker' === $this->kernelEnvironment || ! $event->isMainRequest()) {
            return;
        }

        $this->bugsnag->startSession();
    }

    public function onKernelTerminate(TerminateEvent $event): void
    {
        if ('docker' === $this->kernelEnvironment || ! $event->isMainRequest()) {
            return;
        }

        $this->bugsnag->getSessionTracker()->sendSessions();
    }

    /** @return array<string, array{string, int}|array{string, int}[]|string|string[][]> */
    public static function getSubscribedEvents(): array
    {
        return [
            KernelEvents::REQUEST => ['onKernelRequest', 256],
            KernelEvents::TERMINATE => ['onKernelTerminate', 256],
        ];
    }
}

Recently our application suddenly became much slower on our production environment. Doing some quick profiling revealed that an http call to sessions.bugsnag.com was the root cause of this slowdown (the call took over 6 seconds on average).

Note: this was due to networking issues at digital ocean, not bugsnag itself.

I found this peculiar at first since we call deliverSessions in a kernel.terminate event in Symfony, which in theory means that I would have expected the call to sessions.bugsnag.com to happen after we had already delivered a response to the client.

Doing some quick reviewing of the code however, I learned that simply calling startSession already immediately triggers deliverSessions due to:

startSession calling incrementSessions

$this->incrementSessions($currentTime);

This $lastSent variable defaulting to 0

$lastSent = $this->getLastSent();

Meaning this conditional is always true

if ($deliver && ((time() - $lastSent) > self::$DELIVERY_INTERVAL)) {

Is this expected / intentional behaviour?

Metadata

Metadata

Assignees

No one assigned

    Labels

    backlogWe hope to fix this feature/bug in the future

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions