{"record":{"id":"6f06618533df12a1","repo":"walkor/workerman","slug":"the-outputstream-must-to-be-a-stream-s-given","errorCode":null,"errorMessage":"The $outputStream must to be a stream, %s given","messagePattern":"The \\$outputStream must to be a stream, (.+?) given","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/Worker.php","lineNumber":682,"sourceCode":"            $iniFilePath = (string)php_ini_loaded_file();\n            exit('Notice: '. implode(',', $disabled) . \" are disabled by disable_functions. \" . PHP_EOL\n                . \"Please remove them from disable_functions in $iniFilePath\" . PHP_EOL);\n        }\n    }\n\n    /**\n     * Init stdout.\n     *\n     * @return void\n     */\n    protected static function initStdOut(): void\n    {\n        $defaultStream = fn () => defined('STDOUT') ? STDOUT : (@fopen('php://stdout', 'w') ?: fopen('php://output', 'w'));\n        static::$outputStream ??= $defaultStream(); //@phpstan-ignore-line\n        if (!is_resource(self::$outputStream) || get_resource_type(self::$outputStream) !== 'stream') {\n            $type = get_debug_type(self::$outputStream);\n            static::$outputStream = $defaultStream();\n            throw new RuntimeException(sprintf('The $outputStream must to be a stream, %s given', $type));\n        }\n\n        static::$outputDecorated ??= self::hasColorSupport();\n    }\n\n    /**\n     * Borrowed from the symfony console\n     * @link https://github.com/symfony/console/blob/0d14a9f6d04d4ac38a8cea1171f4554e325dae92/Output/StreamOutput.php#L92\n     */\n    private static function hasColorSupport(): bool\n    {\n        // Follow https://no-color.org/\n        if (getenv('NO_COLOR') !== false) {\n            return false;\n        }\n\n        if (getenv('TERM_PROGRAM') === 'Hyper') {\n            return true;","sourceCodeStart":664,"sourceCodeEnd":700,"githubUrl":"https://github.com/walkor/workerman/blob/1391112a61d23020e11e7b89f17050f6cfaea431/src/Worker.php#L664-L700","documentation":"Worker::initStdOut() validates the configured output stream: it must be a live resource of type 'stream' (the place Workerman prints status/log output to). When the property holds something else (a string path, an array, a closed resource) it resets to the default stream and throws, naming the wrong type via get_debug_type.","triggerScenarios":"Setting Worker::$outputStream = 'php://stdout' or a file path string instead of a resource; assigning the result of a wrapper that returned false/null; passing a resource that was fclose()'d before Worker::runAll(); Worker::setStdOut() called with a non-resource.","commonSituations":"Trying to redirect worker output to a log file by assigning a path string; refactoring that swaps STDOUT (constant, resource) for a configurable value that arrives as a string from config; double-closing the stream in custom shutdown code.","solutions":["Assign a real resource: Worker::setStdOut(fopen('/var/log/app.log', 'a')) or leave it unset to use the default STDOUT","If the stream comes from config, fopen() it first and check the result is a resource before assigning","Do not close the stream you assigned; Workerman owns it for the process lifetime"],"exampleFix":"// before\nWorker::$outputStream = '/var/log/app.log'; // string -> throws 'must be a stream, string given'\n\n// after\nWorker::setStdOut(fopen('/var/log/app.log', 'a'));","handlingStrategy":"validation","validationCode":"$stream = fopen('/var/log/app.log', 'a');\nif ($stream === false || !is_resource($stream) || get_resource_type($stream) !== 'stream') {\n    throw new RuntimeException('cannot open log stream, keeping default stdout');\n}\nWorker::setStdOut($stream);","typeGuard":"function isUsableOutputStream(mixed $s): bool\n{\n    return is_resource($s) && get_resource_type($s) === 'stream';\n}","tryCatchPattern":null,"preventionTips":["Never assign a path string to Worker::$outputStream; fopen() it and check the result first","Let Workerman own the stream: do not fclose() it in your own shutdown code"],"tags":["php","workerman","stdout","logging","configuration"],"backgroundTag":"invalid-stream-resource","analyzedSha":"1391112a61d23020e11e7b89f17050f6cfaea431","analyzedAt":"2026-08-21T02:05:46.744Z","schemaVersion":2},"datasetVersion":"2026-08-21T03:17:12.404Z"}