Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changes/nextrelease/fix-memory-leak.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"type": "enhancement",
"category": "",
"description": "Use WeakReference in PresignUrlMiddleware and EndpointDiscoveryMiddleware to prevent circular reference memory leaks."
}
]
15 changes: 8 additions & 7 deletions src/EndpointDiscovery/EndpointDiscoveryMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class EndpointDiscoveryMiddleware
private static $discoveryCooldown = 60;

private $args;
private $client;
private \WeakReference $client;
private $config;
private $discoveryTimes = [];
private $nextHandler;
Expand All @@ -32,14 +32,15 @@ public static function wrap(
$args,
$config
) {
$clientRef = \WeakReference::create($client);
return function (callable $handler) use (
$client,
$clientRef,
$args,
$config
) {
return new static(
$handler,
$client,
$clientRef->get(),
$args,
$config
);
Expand All @@ -53,7 +54,7 @@ public function __construct(
$config
) {
$this->nextHandler = $handler;
$this->client = $client;
$this->client = \WeakReference::create($client);
$this->args = $args;
$this->service = $client->getApi();
$this->config = $config;
Expand Down Expand Up @@ -91,7 +92,7 @@ public function __invoke(CommandInterface $cmd, RequestInterface $request)
$identifiers = $this->getIdentifiers($op);

$cacheKey = $this->getCacheKey(
$this->client->getCredentials()->wait(),
$this->client->get()->getCredentials()->wait(),
$cmd,
$identifiers
);
Expand Down Expand Up @@ -178,7 +179,7 @@ private function discoverEndpoint(
) {
$discCmd = $this->getDiscoveryCommand($cmd, $identifiers);
$this->discoveryTimes[$cacheKey] = time();
$result = $this->client->execute($discCmd);
$result = $this->client->get()->execute($discCmd);

if (isset($result['Endpoints'])) {
$endpointData = [];
Expand Down Expand Up @@ -237,7 +238,7 @@ private function getDiscoveryCommand(
$params['Identifiers'][$identifier] = $cmd[$identifier];
}
}
$command = $this->client->getCommand($endpointOperation, $params);
$command = $this->client->get()->getCommand($endpointOperation, $params);
$command->getHandlerList()->appendBuild(
Middleware::mapRequest(function (RequestInterface $r) {
return $r->withHeader(
Expand Down
17 changes: 9 additions & 8 deletions src/PresignUrlMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/
class PresignUrlMiddleware
{
private $client;
private \WeakReference $client;
private $endpointProvider;
private $nextHandler;
/** @var array names of operations that require presign url */
Expand All @@ -32,7 +32,7 @@ public function __construct(
callable $nextHandler
) {
$this->endpointProvider = $endpointProvider;
$this->client = $client;
$this->client = \WeakReference::create($client);
$this->nextHandler = $nextHandler;
$this->commandPool = $options['operations'];
$this->serviceName = $options['service'];
Expand All @@ -50,9 +50,9 @@ public static function wrap(
$endpointProvider,
array $options = []
) {
return function (callable $handler) use ($endpointProvider, $client, $options) {
$f = new PresignUrlMiddleware($options, $endpointProvider, $client, $handler);
return $f;
$clientRef = \WeakReference::create($client);
return function (callable $handler) use ($endpointProvider, $clientRef, $options) {
return new PresignUrlMiddleware($options, $endpointProvider, $clientRef->get(), $handler);
};
}

Expand All @@ -61,15 +61,16 @@ public function __invoke(CommandInterface $cmd, ?RequestInterface $request = nul
if (in_array($cmd->getName(), $this->commandPool)
&& (!isset($cmd['__skip' . $cmd->getName()]))
) {
$cmd['DestinationRegion'] = $this->client->getRegion();
$client = $this->client->get();
$cmd['DestinationRegion'] = $client->getRegion();
if (!empty($cmd['SourceRegion']) && !empty($cmd[$this->presignParam])) {
goto nexthandler;
}
if (!$this->requireDifferentRegion
|| (!empty($cmd['SourceRegion'])
&& $cmd['SourceRegion'] !== $cmd['DestinationRegion'])
) {
$cmd[$this->presignParam] = $this->createPresignedUrl($this->client, $cmd);
$cmd[$this->presignParam] = $this->createPresignedUrl($client, $cmd);
}
}
nexthandler:
Expand All @@ -91,7 +92,7 @@ private function createPresignedUrl(
// Create the new endpoint for the target endpoint.
if ($this->endpointProvider instanceof \Aws\EndpointV2\EndpointProviderV2) {
$providerArgs = array_merge(
$this->client->getEndpointProviderArgs(),
$this->client->get()->getEndpointProviderArgs(),
['Region' => $cmd['SourceRegion']]
);
$endpoint = $this->endpointProvider->resolveEndpoint($providerArgs)->getUrl();
Expand Down