From 0488519f39e320d8aa17de544cf3886a6869eeb0 Mon Sep 17 00:00:00 2001 From: maj00r Date: Sat, 14 Nov 2026 00:04:54 +0100 Subject: [PATCH] fix: the simulator opens more than 512 files at once, and a shader it can't open says why With "physicslog yes" every driver keeps its .dat open, and a large scenery (Tomaszewo: some 500 vehicles, cars of the roads included) took all 512 streams the MSVC runtime gives: the next fopen failed and the simulator stopped at "cannot read shader: shaders/map.vert" on opening the map. The limit is raised to 8192 at the start (the soft limit of open files up to the hard one on Linux), and a shader file that doesn't open is told as such, with the system's reason, rather than as one that can't be read. --- EU07.cpp | 24 ++++++++++++++++++++++++ gl/shader.cpp | 8 ++++++++ 2 files changed, 32 insertions(+) diff --git a/EU07.cpp b/EU07.cpp index 20f24aa5a..136882299 100644 --- a/EU07.cpp +++ b/EU07.cpp @@ -27,6 +27,11 @@ Stele, firleju, szociu, hunter, ZiomalCl, OLI_EU and others #include #include #include +#ifdef _WIN32 +#include +#else +#include +#endif #ifdef WITHDUMPGEN #ifdef _WIN32 #include @@ -136,10 +141,29 @@ void finish_log() end_test_run("the simulator stopped before the test ended"); } +// every vehicle with a driver keeps its physics log open for the whole run, and on a scenery with some five hundred +// of them the logs took all the files the C library of Windows lets a process have open by default (512): the +// files opened after them, the shaders of the map among them, couldn't be read. so the limit is raised to the most +// either system allows +void raise_open_files_limit() +{ +#ifdef _WIN32 + _setmaxstdio(8192); +#else + rlimit limit{}; + if (getrlimit(RLIMIT_NOFILE, &limit) == 0 && limit.rlim_cur < limit.rlim_max) + { + limit.rlim_cur = limit.rlim_max; + setrlimit(RLIMIT_NOFILE, &limit); + } +#endif +} + } // namespace int main(int argc, char *argv[]) { + raise_open_files_limit(); #ifdef WITHDUMPGEN #ifdef _WIN32 SetUnhandledExceptionFilter(CrashHandler); diff --git a/gl/shader.cpp b/gl/shader.cpp index 31a3e6a87..c276cdef8 100644 --- a/gl/shader.cpp +++ b/gl/shader.cpp @@ -1,8 +1,10 @@ #include "stdafx.h" +#include #include #include #include +#include #include "shader.h" #include "glsl_common.h" #include "utilities/Logs.h" @@ -58,6 +60,12 @@ std::string gl::shader::read_file(const std::string &filename) const std::ifstream f; f.open(filename); + if (false == f.is_open()) + { + // with the reason the system gives: a file which is there but can't be opened (too many files open) looks + // the same as a missing one otherwise + throw shader_exception("cannot open shader: " + filename + " (" + std::generic_category().message(errno) + ")"); + } stream << f.rdbuf(); f.close();