[PATCH] Ensure sufficient stack space for async threads

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]


The recv_sideband function, which runs in a non-main thread, makes use
of a 64k buffer on the stack. While Linux/glibc systems default to
huge thread stack size (typically 2-10 MB), it's not portable to
assume that the stack for a newly created thread will be able to
support large automatic buffers unless you specifically request space
when creating the thread. The attached patch ensures that at least
128k of stack space is available; certainly that could be increased if
it's deemed safer.

Does this actually matter? Well, the default build of git crashes on
musl libc (www.etalabs.net/musl), where the default stack size is 16k.
We're presently in the process of evaluating what's a good default
stack size for musl to give applications that don't request one
(aiming for a balance that avoids breaking programs like git but also
avoids excess memory usage on tiny embedded systems, a major target
for us) and we'll almost certainly increase the default enough that
the current version of git works without explicitly requesting a stack
size. Nonetheless, this could be an issue again in the future when
running/using git on mobile/embedded-type targets (anybody know
Bionic's default?), and since the patch is simple and has essentially
no cost, I think it's worthwhile to include.

If you have questions please include me in the Cc as I'm not
subscribed.

Rich
--- run-command.c.orig
+++ run-command.c
@@ -637,11 +637,19 @@
 	async->proc_in = proc_in;
 	async->proc_out = proc_out;
 	{
-		int err = pthread_create(&async->tid, NULL, run_thread, async);
+		int err;
+		pthread_attr_t attr;
+		size_t stacksize;
+		pthread_attr_init(&attr);
+		pthread_attr_getstacksize(&attr, &stacksize);
+		if (stacksize < 131072) stacksize = 131072;
+		pthread_attr_setstacksize(&attr, stacksize);
+		err = pthread_create(&async->tid, &attr, run_thread, async);
 		if (err) {
 			error("cannot create thread: %s", strerror(err));
 			goto error;
 		}
+		pthread_attr_destroy(&attr);
 	}
 #endif
 	return 0;

[Newbies FAQ]     [Linux Kernel Development]     [Free Online Dating]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Free Online Dating]     [Photo]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]     [Linux Resources]

Add to Google