Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 559ed47

Browse files
committed
Merge pull request nwjs#698 from GnorTech/master
fix out-of-fd issue on OSX
2 parents 88f0c35 + 8f432d4 commit 559ed47

2 files changed

Lines changed: 49 additions & 0 deletions

File tree

src/shell_browser_main_parts.cc

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "base/threading/thread_restrictions.h"
2828
#include "base/utf_string_conversions.h"
2929
#include "base/values.h"
30+
#include "chrome/common/chrome_switches.h"
3031
#include "content/nw/src/api/app/app.h"
3132
#include "content/nw/src/browser/printing/print_job_manager.h"
3233
#include "content/nw/src/browser/shell_devtools_delegate.h"
@@ -41,12 +42,37 @@
4142
#include "net/proxy/proxy_resolver_v8.h"
4243
#include "ui/base/resource/resource_bundle.h"
4344

45+
#if !defined(OS_WIN)
46+
#include <sys/resource.h>
47+
#endif
48+
4449
#if defined(TOOLKIT_GTK)
4550
#include "content/nw/src/browser/printing/print_dialog_gtk.h"
4651
#endif
4752

4853
namespace {
4954

55+
#if !defined(OS_WIN)
56+
// Sets the file descriptor soft limit to |max_descriptors| or the OS hard
57+
// limit, whichever is lower.
58+
void SetFileDescriptorLimit(unsigned int max_descriptors) {
59+
struct rlimit limits;
60+
if (getrlimit(RLIMIT_NOFILE, &limits) == 0) {
61+
unsigned int new_limit = max_descriptors;
62+
if (limits.rlim_max > 0 && limits.rlim_max < max_descriptors) {
63+
new_limit = limits.rlim_max;
64+
}
65+
limits.rlim_cur = new_limit;
66+
if (setrlimit(RLIMIT_NOFILE, &limits) != 0) {
67+
PLOG(INFO) << "Failed to set file descriptor limit";
68+
}
69+
} else {
70+
PLOG(INFO) << "Failed to get file descriptor limit";
71+
}
72+
}
73+
74+
#endif
75+
5076
base::StringPiece PlatformResourceProvider(int key) {
5177
if (key == IDR_DIR_HEADER_HTML) {
5278
base::StringPiece html_data =
@@ -203,4 +229,26 @@ printing::PrintJobManager* ShellBrowserMainParts::print_job_manager() {
203229
return print_job_manager_.get();
204230
}
205231

232+
void ShellBrowserMainParts::PreEarlyInitialization() {
233+
#if !defined(OS_WIN)
234+
// see chrome_browser_main_posix.cc
235+
CommandLine& command_line = *CommandLine::ForCurrentProcess();
236+
const std::string fd_limit_string =
237+
command_line.GetSwitchValueASCII(switches::kFileDescriptorLimit);
238+
int fd_limit = 0;
239+
if (!fd_limit_string.empty()) {
240+
base::StringToInt(fd_limit_string, &fd_limit);
241+
}
242+
#if defined(OS_MACOSX)
243+
// We use quite a few file descriptors for our IPC, and the default limit on
244+
// the Mac is low (256), so bump it up if there is no explicit override.
245+
if (fd_limit == 0) {
246+
fd_limit = 1024;
247+
}
248+
#endif // OS_MACOSX
249+
if (fd_limit > 0)
250+
SetFileDescriptorLimit(fd_limit);
251+
#endif // !OS_WIN
252+
}
253+
206254
} // namespace content

src/shell_browser_main_parts.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class ShellBrowserMainParts : public BrowserMainParts {
3838
virtual ~ShellBrowserMainParts();
3939

4040
// BrowserMainParts overrides.
41+
virtual void PreEarlyInitialization() OVERRIDE;
4142
virtual void PreMainMessageLoopStart() OVERRIDE;
4243
virtual void PreMainMessageLoopRun() OVERRIDE;
4344
virtual void PostMainMessageLoopStart() OVERRIDE;

0 commit comments

Comments
 (0)