kaxil opened a new pull request, #55443:
URL: https://github.com/apache/airflow/pull/55443

   Final blocker for 3.1.0b1
   
   The `airflow scheduler` fails to start with this error when the logs 
directory doesn't exist:
   
   ```
   RuntimeError: Directory '/root/airflow/logs' does not exist
   ```
   
   This regression was introduced in 
[#52581](https://github.com/apache/airflow/pull/52581) when serve_logs was 
refactored from Flask to FastAPI.
   
   **Module Import Order Dependency**: FastAPI's `StaticFiles` validates 
directory existence at app creation time, but this happens at **module import 
time** before Airflow's initialization sequence:
   
   ```python
   uvicorn.run("airflow.utils.serve_logs.log_server:app")  # imports module
   ↓
   app = create_app()  # runs immediately at module level
   ↓
   StaticFiles(directory=log_directory)  # FAILS: directory doesn't exist yet
   ```
   
   **Expected initialization order**:
   ```
   settings.initialize() → configure_logging() → init_log_folder() → 
create_app()
   ```
   
   **Actual order with eager loading**:
   ```
   import log_server → create_app() → FAIL (directory doesn't exist)
   ```
   
   ---
   Without this fix, we see following error when running `airflow standalone` 
fresh in an isolated container:
   
   Error 1:
   
   ```
   scheduler  | Traceback (most recent call last):
   scheduler  | File "/.venv/bin/airflow", line 10, in <module>
   scheduler  | sys.exit(main())
   scheduler  | File "/.venv/lib/python3.10/site-packages/airflow/__main__.py", 
line 55, in main
   scheduler  | args.func(args)
   scheduler  | File 
"/.venv/lib/python3.10/site-packages/airflow/cli/cli_config.py", line 49, in 
command
   scheduler  | return func(*args, **kwargs)
   scheduler  | File 
"/.venv/lib/python3.10/site-packages/airflow/utils/cli.py", line 114, in wrapper
   scheduler  | return f(*args, **kwargs)
   scheduler  | File 
"/.venv/lib/python3.10/site-packages/airflow/utils/providers_configuration_loader.py",
 line 54, in wrapped_function
   scheduler  | return func(*args, **kwargs)
   scheduler  | File 
"/.venv/lib/python3.10/site-packages/airflow/cli/commands/scheduler_command.py",
 line 52, in scheduler
   scheduler  | run_command_with_daemon_option(
   scheduler  | File 
"/.venv/lib/python3.10/site-packages/airflow/cli/commands/daemon_utils.py", 
line 86, in run_command_with_daemon_option
   scheduler  | callback()
   scheduler  | File 
"/.venv/lib/python3.10/site-packages/airflow/cli/commands/scheduler_command.py",
 line 55, in <lambda>
   scheduler  | callback=lambda: _run_scheduler_job(args),
   scheduler  | File 
"/.venv/lib/python3.10/site-packages/airflow/cli/commands/scheduler_command.py",
 line 42, in _run_scheduler_job
   scheduler  | with _serve_logs(args.skip_serve_logs), 
_serve_health_check(enable_health_check):
   scheduler  | File "/usr/local/lib/python3.10/contextlib.py", line 135, in 
__enter__
   scheduler  | return next(self.gen)
   scheduler  | File 
"/.venv/lib/python3.10/site-packages/airflow/cli/commands/scheduler_command.py",
 line 62, in _serve_logs
   scheduler  | from airflow.utils.serve_logs import serve_logs
   scheduler  | File 
"/.venv/lib/python3.10/site-packages/airflow/utils/serve_logs/__init__.py", 
line 20, in <module>
   scheduler  | from airflow.utils.serve_logs.log_server import create_app
   scheduler  | File 
"/.venv/lib/python3.10/site-packages/airflow/utils/serve_logs/log_server.py", 
line 160, in <module>
   scheduler  | app = create_app()
   scheduler  | File 
"/.venv/lib/python3.10/site-packages/airflow/utils/serve_logs/log_server.py", 
line 153, in create_app
   scheduler  | JWTAuthStaticFiles(directory=log_directory, html=False),
   scheduler  | File 
"/.venv/lib/python3.10/site-packages/airflow/utils/serve_logs/log_server.py", 
line 49, in __init__
   scheduler  | super().__init__(*args, **kwargs)
   scheduler  | File 
"/.venv/lib/python3.10/site-packages/starlette/staticfiles.py", line 56, in 
__init__
   scheduler  | raise RuntimeError(f"Directory '{directory}' does not exist")
   scheduler  | RuntimeError: Directory '/root/airflow/logs' does not exist
   ```
   
   Error 2:
   
   This was because we had harcoded 2 workers! but this isn't needed
   ```
   scheduler  | Process SpawnProcess-1:53:
   scheduler  | Traceback (most recent call last):
   scheduler  | File "/usr/local/lib/python3.10/multiprocessing/process.py", 
line 314, in _bootstrap
   scheduler  | self.run()
   scheduler  | File "/usr/local/lib/python3.10/multiprocessing/process.py", 
line 108, in run
   scheduler  | self._target(*self._args, **self._kwargs)
   scheduler  | File 
"/.venv/lib/python3.10/site-packages/uvicorn/_subprocess.py", line 73, in 
subprocess_started
   scheduler  | sys.stdin = os.fdopen(stdin_fileno)  # pragma: full coverage
   scheduler  | File "/usr/local/lib/python3.10/os.py", line 1030, in fdopen
   scheduler  | return io.open(fd, mode, buffering, encoding, *args, **kwargs)
   scheduler  | OSError: [Errno 9] Bad file descriptor
   scheduler  | Process SpawnProcess-1:54:
   scheduler  | Traceback (most recent call last):
   scheduler  | File "/usr/local/lib/python3.10/multiprocessing/process.py", 
line 314, in _bootstrap
   scheduler  | self.run()
   scheduler  | File "/usr/local/lib/python3.10/multiprocessing/process.py", 
line 108, in run
   scheduler  | self._target(*self._args, **self._kwargs)
   scheduler  | File 
"/.venv/lib/python3.10/site-packages/uvicorn/_subprocess.py", line 73, in 
subprocess_started
   scheduler  | sys.stdin = os.fdopen(stdin_fileno)  # pragma: full coverage
   scheduler  | File "/usr/local/lib/python3.10/os.py", line 1030, in fdopen
   scheduler  | return io.open(fd, mode, buffering, encoding, *args, **kwargs)
   scheduler  | OSError: [Errno 9] Bad file descriptor
   triggerer  | Process SpawnProcess-1:53:
   triggerer  | Traceback (most recent call last):
   triggerer  | File "/usr/local/lib/python3.10/multiprocessing/process.py", 
line 314, in _bootstrap
   triggerer  | self.run()
   triggerer  | File "/usr/local/lib/python3.10/multiprocessing/process.py", 
line 108, in run
   triggerer  | self._target(*self._args, **self._kwargs)
   triggerer  | File 
"/.venv/lib/python3.10/site-packages/uvicorn/_subprocess.py", line 73, in 
subprocess_started
   triggerer  | sys.stdin = os.fdopen(stdin_fileno)  # pragma: full coverage
   triggerer  | File "/usr/local/lib/python3.10/os.py", line 1030, in fdopen
   triggerer  | return io.open(fd, mode, buffering, encoding, *args, **kwargs)
   triggerer  | OSError: [Errno 9] Bad file descriptor
   ```
   
   This implementation follows the same pattern as Airflow's main API:
   
   | Component | Pattern | File |
   |-----------|---------|------|
   | **Main API** | `cached_app()` with global caching | `api_fastapi/app.py` | 
| **serve_logs** | `get_app()` with global caching | 
`utils/serve_logs/log_server.py` |
   
   Both use lazy initialization with global app caching, ensuring proper 
startup order while maintaining performance.
   
   ---
   
   **Fixes**: Scheduler startup failures with LocalExecutor
   **Related-to**: d8a3ad24df5ae363b7dee3441fac1b4c8df85c3a
   
   <!--
    Licensed to the Apache Software Foundation (ASF) under one
    or more contributor license agreements.  See the NOTICE file
    distributed with this work for additional information
    regarding copyright ownership.  The ASF licenses this file
    to you under the Apache License, Version 2.0 (the
    "License"); you may not use this file except in compliance
    with the License.  You may obtain a copy of the License at
   
      http://www.apache.org/licenses/LICENSE-2.0
   
    Unless required by applicable law or agreed to in writing,
    software distributed under the License is distributed on an
    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    KIND, either express or implied.  See the License for the
    specific language governing permissions and limitations
    under the License.
    -->
   
   <!--
   Thank you for contributing! Please make sure that your code changes
   are covered with tests. And in case of new features or big changes
   remember to adjust the documentation.
   
   Feel free to ping committers for the review!
   
   In case of an existing issue, reference it using one of the following:
   
   closes: #ISSUE
   related: #ISSUE
   
   How to write a good git commit message:
   http://chris.beams.io/posts/git-commit/
   -->
   
   
   
   <!-- Please keep an empty line above the dashes. -->
   ---
   **^ Add meaningful description above**
   Read the **[Pull Request 
Guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#pull-request-guidelines)**
 for more information.
   In case of fundamental code changes, an Airflow Improvement Proposal 
([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvement+Proposals))
 is needed.
   In case of a new dependency, check compliance with the [ASF 3rd Party 
License Policy](https://www.apache.org/legal/resolved.html#category-x).
   In case of backwards incompatible changes please leave a note in a 
newsfragment file, named `{pr_number}.significant.rst` or 
`{issue_number}.significant.rst`, in 
[airflow-core/newsfragments](https://github.com/apache/airflow/tree/main/airflow-core/newsfragments).
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to