1 | NAME |
1 | NAME |
2 | AnyEvent::Fork - everything you wanted to use fork() for, but couldn't |
2 | AnyEvent::Fork - everything you wanted to use fork() for, but couldn't |
3 | |
|
|
4 | ATTENTION, this is a very early release, and very untested. Consider it |
|
|
5 | a technology preview. |
|
|
6 | |
3 | |
7 | SYNOPSIS |
4 | SYNOPSIS |
8 | use AnyEvent::Fork; |
5 | use AnyEvent::Fork; |
9 | |
6 | |
10 | ################################################################## |
7 | ################################################################## |
… | |
… | |
357 | my ($fh, $str1, $str2, $fh1, $fh2, $str3) = @_; |
354 | my ($fh, $str1, $str2, $fh1, $fh2, $str3) = @_; |
358 | |
355 | |
359 | print scalar <$fh>; # prints "hi 1\n" and "hi 2\n" |
356 | print scalar <$fh>; # prints "hi 1\n" and "hi 2\n" |
360 | } |
357 | } |
361 | |
358 | |
|
|
359 | TYPICAL PROBLEMS |
|
|
360 | This section lists typical problems that remain. I hope by recognising |
|
|
361 | them, most can be avoided. |
|
|
362 | |
|
|
363 | "leaked" file descriptors for exec'ed processes |
|
|
364 | POSIX systems inherit file descriptors by default when exec'ing a |
|
|
365 | new process. While perl itself laudably sets the close-on-exec flags |
|
|
366 | on new file handles, most C libraries don't care, and even if all |
|
|
367 | cared, it's often not possible to set the flag in a race-free |
|
|
368 | manner. |
|
|
369 | |
|
|
370 | That means some file descriptors can leak through. And since it |
|
|
371 | isn't possible to know which file descriptors are "good" and |
|
|
372 | "neccessary" (or even to know which file descreiptors are open), |
|
|
373 | there is no good way to close the ones that might harm. |
|
|
374 | |
|
|
375 | As an example of what "harm" can be done consider a web server that |
|
|
376 | accepts connections and afterwards some module uses AnyEvent::Fork |
|
|
377 | for the first time, causing it to fork and exec a new process, which |
|
|
378 | might inherit the network socket. When the server closes the socket, |
|
|
379 | it is still open in the child (which doesn't even know that) and the |
|
|
380 | client might conclude that the connection is still fine. |
|
|
381 | |
|
|
382 | For the main program, there are multiple remedies available - |
|
|
383 | AnyEvent::Fork::Early is one, creating a process early and not using |
|
|
384 | "new_exec" is another, as in both cases, the first process can be |
|
|
385 | exec'ed well before many random file descriptors are open. |
|
|
386 | |
|
|
387 | In general, the solution for these kind of problems is to fix the |
|
|
388 | libraries or the code that leaks those file descriptors. |
|
|
389 | |
|
|
390 | Fortunately, most of these lekaed descriptors do no harm, other than |
|
|
391 | sitting on some resources. |
|
|
392 | |
|
|
393 | "leaked" file descriptors for fork'ed processes |
|
|
394 | Normally, AnyEvent::Fork does start new processes by exec'ing them, |
|
|
395 | which closes file descriptors not marked for being inherited. |
|
|
396 | |
|
|
397 | However, AnyEvent::Fork::Early and AnyEvent::Fork::Template offer a |
|
|
398 | way to create these processes by forking, and this leaks more file |
|
|
399 | descriptors than exec'ing them, as there is no way to mark |
|
|
400 | descriptors as "close on fork". |
|
|
401 | |
|
|
402 | An example would be modules like EV, IO::AIO or Gtk2. Both create |
|
|
403 | pipes for internal uses, and Gtk2 might open a connection to the X |
|
|
404 | server. EV and IO::AIO can deal with fork, but Gtk2 might have |
|
|
405 | trouble with a fork. |
|
|
406 | |
|
|
407 | The solution is to either not load these modules before use'ing |
|
|
408 | AnyEvent::Fork::Early or AnyEvent::Fork::Template, or to delay |
|
|
409 | initialising them, for example, by calling "init Gtk2" manually. |
|
|
410 | |
362 | PORTABILITY NOTES |
411 | PORTABILITY NOTES |
363 | Native win32 perls are somewhat supported (AnyEvent::Fork::Early is a |
412 | Native win32 perls are somewhat supported (AnyEvent::Fork::Early is a |
364 | nop, and ::Template is not going to work), and it cost a lot of blood |
413 | nop, and ::Template is not going to work), and it cost a lot of blood |
365 | and sweat to make it so, mostly due to the bloody broken perl that |
414 | and sweat to make it so, mostly due to the bloody broken perl that |
366 | nobody seems to care about. The fork emulation is a bad joke - I have |
415 | nobody seems to care about. The fork emulation is a bad joke - I have |
… | |
… | |
369 | |
418 | |
370 | Cygwin perl is not supported at the moment, as it should implement fd |
419 | Cygwin perl is not supported at the moment, as it should implement fd |
371 | passing, but doesn't, and rolling my own is hard, as cygwin doesn't |
420 | passing, but doesn't, and rolling my own is hard, as cygwin doesn't |
372 | support enough functionality to do it. |
421 | support enough functionality to do it. |
373 | |
422 | |
|
|
423 | SEE ALSO |
|
|
424 | AnyEvent::Fork::Early (to avoid executing a perl interpreter), |
|
|
425 | AnyEvent::Fork::Template (to create a process by forking the main |
|
|
426 | program at a convenient time). |
|
|
427 | |
374 | AUTHOR |
428 | AUTHOR |
375 | Marc Lehmann <schmorp@schmorp.de> |
429 | Marc Lehmann <schmorp@schmorp.de> |
376 | http://home.schmorp.de/ |
430 | http://home.schmorp.de/ |
377 | |
431 | |