Skip to content

Commit

Permalink
Fix type error if config loading results in exception (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
kelunik authored Jan 21, 2023
1 parent 12884f8 commit f528711
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 27 deletions.
3 changes: 3 additions & 0 deletions src/DnsConfigLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@

interface DnsConfigLoader
{
/**
* @throws DnsConfigException
*/
public function loadConfig(): DnsConfig;
}
61 changes: 34 additions & 27 deletions src/Rfc1035StubDnsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,7 @@ public function resolve(string $name, ?int $typeRestriction = null, ?Cancellatio
default => throw new \Error("Invalid value for parameter 2: null|Record::A|Record::AAAA expected"),
};

if ($this->configStatus === self::CONFIG_NOT_LOADED) {
$this->reloadConfig();
}

$this->loadConfigIfNotLoaded();
if ($this->configStatus === self::CONFIG_FAILED) {
return $this->blockingFallbackResolver->resolve($name, $typeRestriction, $cancellation);
}
Expand Down Expand Up @@ -218,6 +215,8 @@ public function resolve(string $name, ?int $typeRestriction = null, ?Cancellatio
* Reloads the configuration in the background.
*
* Once it's finished, the configuration will be used for new requests.
*
* @throws DnsConfigException
*/
public function reloadConfig(): DnsConfig
{
Expand All @@ -232,29 +231,11 @@ public function reloadConfig(): DnsConfig
} catch (DnsConfigException $e) {
$this->configStatus = self::CONFIG_FAILED;

$message = "Could not load the system's DNS configuration; "
. "falling back to synchronous, blocking resolver; "
. \get_class($e) . ": " . $e->getMessage();

try {
\trigger_error(
$message,
\E_USER_WARNING
);
} catch (\Throwable) {
\set_error_handler(null);
\trigger_error(
$message,
\E_USER_WARNING
);
\restore_error_handler();
}
throw $e;
} finally {
$this->pendingConfig = null;
}

\assert($this->config !== null);

return $this->config;
});

Expand All @@ -271,10 +252,7 @@ public function query(string $name, int $type, ?Cancellation $cancellation = nul

$future = async(function () use ($name, $type, $cancellation): array {
try {
if ($this->configStatus === self::CONFIG_NOT_LOADED) {
$this->reloadConfig();
}

$this->loadConfigIfNotLoaded();
if ($this->configStatus === self::CONFIG_FAILED) {
return $this->blockingFallbackResolver->query($name, $type, $cancellation);
}
Expand Down Expand Up @@ -558,4 +536,33 @@ private function shouldRetry(int|string $code): bool
MessageResponseCodes::NAME_ERROR,
], true);
}

private function loadConfigIfNotLoaded(): void
{
if ($this->configStatus !== self::CONFIG_NOT_LOADED) {
return;
}

try {
$this->reloadConfig();
} catch (DnsConfigException $e) {
$message = "Could not load the system's DNS configuration; "
. "falling back to synchronous, blocking resolver; "
. \get_class($e) . ": " . $e->getMessage();

try {
\trigger_error(
$message,
\E_USER_WARNING
);
} catch (\Throwable) {
\set_error_handler(null);
\trigger_error(
$message,
\E_USER_WARNING
);
\restore_error_handler();
}
}
}
}

0 comments on commit f528711

Please sign in to comment.