Skip to content

Commit 8fa1702

Browse files
committed
Improve pledge() and avoid CUDA exit() in server
1 parent 59a5d97 commit 8fa1702

File tree

3 files changed

+25
-18
lines changed

3 files changed

+25
-18
lines changed

llama.cpp/ggml-backend.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2248,7 +2248,7 @@ bool ggml_backend_compare_graph_backend(ggml_backend_t backend1, ggml_backend_t
22482248
}
22492249

22502250
GGML_CALL static void system_exit(int rc) {
2251-
exit(rc);
2251+
pthread_exit(0);
22522252
}
22532253

22542254
GGML_CALL static void system_free(void *p) {

llamafile/server/main.cpp

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -105,22 +105,6 @@ main(int argc, char* argv[])
105105
for (int i = 0; i < FLAG_workers; ++i)
106106
npassert(!g_server->spawn());
107107

108-
// install security
109-
if (!FLAG_unsecure) {
110-
const char* promises;
111-
if (FLAG_www_root) {
112-
promises = "stdio anet rpath";
113-
} else {
114-
promises = "stdio anet";
115-
}
116-
if (pledge(0, 0)) {
117-
SLOG("warning: this OS doesn't support pledge() security");
118-
} else if (pledge("stdio anet", 0)) {
119-
perror("pledge");
120-
exit(1);
121-
}
122-
}
123-
124108
// run server
125109
signals_init();
126110
llama_backend_init();

llamafile/server/worker.cpp

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@
2424
#include "llamafile/server/tokenbucket.h"
2525
#include "llamafile/threadlocal.h"
2626
#include "llamafile/trust.h"
27-
#include <cosmo.h>
27+
#include <atomic>
2828
#include <cassert>
29+
#include <cosmo.h>
2930
#include <exception>
3031
#include <pthread.h>
3132

@@ -135,6 +136,28 @@ Worker::handle()
135136
void
136137
Worker::run()
137138
{
139+
if (!FLAG_unsecure) {
140+
static std::atomic<bool> once;
141+
if (llamafile_has_gpu()) {
142+
if (!once.exchange(true))
143+
SLOG("warning: gpu mode disables pledge security");
144+
} else {
145+
const char* promises;
146+
if (FLAG_www_root && !startswith(FLAG_www_root, "/zip/")) {
147+
promises = "stdio anet rpath";
148+
} else {
149+
promises = "stdio anet";
150+
}
151+
if (pledge(0, 0)) {
152+
if (!once.exchange(true))
153+
SLOG("warning: this OS doesn't support pledge() security");
154+
} else if (pledge(promises, 0)) {
155+
perror("pledge");
156+
exit(1);
157+
}
158+
}
159+
}
160+
138161
server_->lock();
139162
dll_make_first(&server_->idle_workers, &elem_);
140163
server_->worker_count.fetch_add(1, std::memory_order_acq_rel);

0 commit comments

Comments
 (0)