{"thread":{"id":"30316","subject":"[PATCH] Ensure sufficient stack space for async threads","startedAt":"2012-04-24T01:55:23Z","lastAt":"2012-04-24T01:55:23Z","messageCount":1,"participants":["Rich Felker"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"189925","messageId":"20120424015523.GA15287@brightrain.aerifal.cx","threadId":"30316","inReplyTo":null,"subject":"[PATCH] Ensure sufficient stack space for async threads","fromName":"Rich Felker","fromEmail":"dalias@aerifal.cx","sentAt":"2012-04-24T01:55:23Z","receivedAt":"2012-04-24T01:55:23Z","isPatch":true,"sender":{"key":"dalias@aerifal.cx","avatar":null},"body":"The recv_sideband function, which runs in a non-main thread, makes use\nof a 64k buffer on the stack. While Linux/glibc systems default to\nhuge thread stack size (typically 2-10 MB), it's not portable to\nassume that the stack for a newly created thread will be able to\nsupport large automatic buffers unless you specifically request space\nwhen creating the thread. The attached patch ensures that at least\n128k of stack space is available; certainly that could be increased if\nit's deemed safer.\n\nDoes this actually matter? Well, the default build of git crashes on\nmusl libc (www.etalabs.net/musl), where the default stack size is 16k.\nWe're presently in the process of evaluating what's a good default\nstack size for musl to give applications that don't request one\n(aiming for a balance that avoids breaking programs like git but also\navoids excess memory usage on tiny embedded systems, a major target\nfor us) and we'll almost certainly increase the default enough that\nthe current version of git works without explicitly requesting a stack\nsize. Nonetheless, this could be an issue again in the future when\nrunning/using git on mobile/embedded-type targets (anybody know\nBionic's default?), and since the patch is simple and has essentially\nno cost, I think it's worthwhile to include.\n\nIf you have questions please include me in the Cc as I'm not\nsubscribed.\n\nRich\n\n\n--- run-command.c.orig\n+++ run-command.c\n@@ -637,11 +637,19 @@\n \tasync->proc_in = proc_in;\n \tasync->proc_out = proc_out;\n \t{\n-\t\tint err = pthread_create(&async->tid, NULL, run_thread, async);\n+\t\tint err;\n+\t\tpthread_attr_t attr;\n+\t\tsize_t stacksize;\n+\t\tpthread_attr_init(&attr);\n+\t\tpthread_attr_getstacksize(&attr, &stacksize);\n+\t\tif (stacksize < 131072) stacksize = 131072;\n+\t\tpthread_attr_setstacksize(&attr, stacksize);\n+\t\terr = pthread_create(&async->tid, &attr, run_thread, async);\n \t\tif (err) {\n \t\t\terror(\"cannot create thread: %s\", strerror(err));\n \t\t\tgoto error;\n \t\t}\n+\t\tpthread_attr_destroy(&attr);\n \t}\n #endif\n \treturn 0;\n"}]}