Moby Project - a collaborative project for the container ecosystem to assemble container-based systems
Go to file
Kir Kolyshkin 916eabd459 daemon.ContainerLogs(): fix resource leak on follow
When daemon.ContainerLogs() is called with options.follow=true
(as in "docker logs --follow"), the "loggerutils.followLogs()"
function never returns (even then the logs consumer is gone).
As a result, all the resources associated with it (including
an opened file descriptor for the log file being read, two FDs
for a pipe, and two FDs for inotify watch) are never released.

If this is repeated (such as by running "docker logs --follow"
and pressing Ctrl-C a few times), this results in DoS caused by
either hitting the limit of inotify watches, or the limit of
opened files. The only cure is daemon restart.

Apparently, what happens is:

1. logs producer (a container) is gone, calling (*LogWatcher).Close()
for all its readers (daemon/logger/jsonfilelog/jsonfilelog.go:175).

2. WatchClose() is properly handled by a dedicated goroutine in
followLogs(), cancelling the context.

3. Upon receiving the ctx.Done(), the code in followLogs()
(daemon/logger/loggerutils/logfile.go#L626-L638) keeps to
send messages _synchronously_ (which is OK for now).

4. Logs consumer is gone (Ctrl-C is pressed on a terminal running
"docker logs --follow"). Method (*LogWatcher).Close() is properly
called (see daemon/logs.go:114). Since it was called before and
due to to once.Do(), nothing happens (which is kinda good, as
otherwise it will panic on closing a closed channel).

5. A goroutine (see item 3 above) keeps sending log messages
synchronously to the logWatcher.Msg channel. Since the
channel reader is gone, the channel send operation blocks forever,
and resource cleanup set up in defer statements at the beginning
of followLogs() never happens.

Alas, the fix is somewhat complicated:

1. Distinguish between close from logs producer and logs consumer.
To that effect,
 - yet another channel is added to LogWatcher();
 - {Watch,}Close() are renamed to {Watch,}ProducerGone();
 - {Watch,}ConsumerGone() are added;

*NOTE* that ProducerGone()/WatchProducerGone() pair is ONLY needed
in order to stop ConsumerLogs(follow=true) when a container is stopped;
otherwise we're not interested in it. In other words, we're only
using it in followLogs().

2. Code that was doing (logWatcher*).Close() is modified to either call
ProducerGone() or ConsumerGone(), depending on the context.

3. Code that was waiting for WatchClose() is modified to wait for
either ConsumerGone() or ProducerGone(), or both, depending on the
context.

4. followLogs() are modified accordingly:
 - context cancellation is happening on WatchProducerGone(),
and once it's received the FileWatcher is closed and waitRead()
returns errDone on EOF (i.e. log rotation handling logic is disabled);
 - due to this, code that was writing synchronously to logWatcher.Msg
can be and is removed as the code above it handles this case;
 - function returns once ConsumerGone is received, freeing all the
resources -- this is the bugfix itself.

While at it,

1. Let's also remove the ctx usage to simplify the code a bit.
It was introduced by commit a69a59ffc7 ("Decouple removing the
fileWatcher from reading") in order to fix a bug. The bug was actually
a deadlock in fsnotify, and the fix was just a workaround. Since then
the fsnofify bug has been fixed, and a new fsnotify was vendored in.
For more details, please see
https://github.com/moby/moby/pull/27782#issuecomment-416794490

2. Since `(*filePoller).Close()` is fixed to remove all the files
being watched, there is no need to explicitly call
fileWatcher.Remove(name) anymore, so get rid of the extra code.

Should fix https://github.com/moby/moby/issues/37391

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2018-09-06 11:47:42 -07:00
.github
api Merge pull request #37690 from KimMachineGun/modify-context-key 2018-09-06 18:45:41 +01:00
builder builder: implement ref checker 2018-09-01 23:40:06 +00:00
cli Various code-cleanup 2018-05-23 17:50:54 +02:00
client client: dial tls on Dialer if tls config is set 2018-09-04 17:43:33 -07:00
cmd/dockerd allow features option live reloadable 2018-08-31 12:43:04 -07:00
container Merge pull request #37092 from cpuguy83/local_logger 2018-08-20 07:01:41 +01:00
contrib Removed the "-i -t" arguments from the smoke test calling printf (these flags seem not really needed, and break jenkins builds with error "the input device is not a TTY") 2018-07-05 17:29:17 +02:00
daemon daemon.ContainerLogs(): fix resource leak on follow 2018-09-06 11:47:42 -07:00
distribution Merge pull request #37359 from dmcgowan/register-oci-mediatypes 2018-07-02 14:26:36 +02:00
dockerversion api/server, dockerversion: modify context key 2018-08-22 11:20:22 +09:00
docs Changes to cluster/filter, swagger.yaml, version-history.md for filtering on node labels. 2018-08-21 22:17:23 +02:00
errdefs Fix the several typos detected by github.com/client9/misspell 2018-08-09 00:45:00 +09:00
hack hack/make.ps1: know where we failed 2018-08-27 18:04:57 -07:00
image system: add back lcow validation function 2018-06-27 15:24:26 -07:00
integration builder: add prune options to the API 2018-09-01 22:01:17 +00:00
integration-cli Windows: Go1.11: Use long path names in build context (TestBuildSymlinkBreakout) 2018-09-05 17:01:05 -07:00
internal Use errors.Wrap() in daemon errors, and cleanup messages 2018-08-23 16:12:44 +02:00
layer Add ADD/COPY --chown flag support to Windows 2018-08-13 21:59:11 -07:00
libcontainerd Fix supervisor healthcheck throttling 2018-09-04 11:00:28 -07:00
migrate/v1 Various code-cleanup 2018-05-23 17:50:54 +02:00
oci Add /proc/acpi to masked paths 2018-07-05 17:39:52 +02:00
opts Update tests to use gotest.tools 👼 2018-06-13 09:04:30 +02:00
pkg Windows: Go1.11: Use long path names in build context (TestBuildSymlinkBreakout) 2018-09-05 17:01:05 -07:00
plugin Merge pull request #35521 from salah-khan/35507 2018-08-17 11:31:16 -07:00
profiles Whitelist syscalls linked to CAP_SYS_NICE in default seccomp profile 2018-06-20 07:32:08 -05:00
project Remove references to old release process 2018-05-18 18:28:43 +00:00
reference Update tests to use gotest.tools 👼 2018-06-13 09:04:30 +02:00
registry Update tests to use gotest.tools 👼 2018-06-13 09:04:30 +02:00
reports Fix typos 2018-05-16 09:15:43 +08:00
restartmanager
runconfig Update tests to use gotest.tools 👼 2018-06-13 09:04:30 +02:00
vendor vendor buildkit to fix a couple of bugs 2018-09-04 15:17:40 +00:00
volume Add ADD/COPY --chown flag support to Windows 2018-08-13 21:59:11 -07:00
.DEREK.yml
.dockerignore
.gitignore
.mailmap Update authors and mailmap 2018-05-21 01:07:22 +02:00
AUTHORS Update authors and mailmap 2018-05-21 01:07:22 +02:00
CHANGELOG.md Re-order CHANGELOG.md to pass `make validate` test 2018-05-13 00:27:51 +00:00
CONTRIBUTING.md Fix link anchors in CONTRIBUTING.md 2018-06-13 21:58:48 +09:00
Dockerfile Bump Go to 1.10.4 2018-08-28 10:08:30 +02:00
Dockerfile.e2e Bump Go to 1.10.4 2018-08-28 10:08:30 +02:00
Dockerfile.simple Bump Go to 1.10.4 2018-08-28 10:08:30 +02:00
Dockerfile.windows Bump Go to 1.10.4 2018-08-28 10:08:30 +02:00
LICENSE
MAINTAINERS Fix the several typos detected by github.com/client9/misspell 2018-08-09 00:45:00 +09:00
Makefile Expose license status in Info (#37612) 2018-08-17 17:05:21 -07:00
NOTICE
README.md
ROADMAP.md
TESTING.md Merge pull request #37249 from AntaresS/add-test-guidline 2018-06-25 20:44:42 +02:00
VENDORING.md
codecov.yml
poule.yml
vendor.conf vendor buildkit to fix a couple of bugs 2018-09-04 15:17:40 +00:00

README.md

The Moby Project

Moby Project logo

Moby is an open-source project created by Docker to enable and accelerate software containerization.

It provides a "Lego set" of toolkit components, the framework for assembling them into custom container-based systems, and a place for all container enthusiasts and professionals to experiment and exchange ideas. Components include container build tools, a container registry, orchestration tools, a runtime and more, and these can be used as building blocks in conjunction with other tools and projects.

Principles

Moby is an open project guided by strong principles, aiming to be modular, flexible and without too strong an opinion on user experience. It is open to the community to help set its direction.

  • Modular: the project includes lots of components that have well-defined functions and APIs that work together.
  • Batteries included but swappable: Moby includes enough components to build fully featured container system, but its modular architecture ensures that most of the components can be swapped by different implementations.
  • Usable security: Moby provides secure defaults without compromising usability.
  • Developer focused: The APIs are intended to be functional and useful to build powerful tools. They are not necessarily intended as end user tools but as components aimed at developers. Documentation and UX is aimed at developers not end users.

Audience

The Moby Project is intended for engineers, integrators and enthusiasts looking to modify, hack, fix, experiment, invent and build systems based on containers. It is not for people looking for a commercially supported system, but for people who want to work and learn with open source code.

Relationship with Docker

The components and tools in the Moby Project are initially the open source components that Docker and the community have built for the Docker Project. New projects can be added if they fit with the community goals. Docker is committed to using Moby as the upstream for the Docker Product. However, other projects are also encouraged to use Moby as an upstream, and to reuse the components in diverse ways, and all these uses will be treated in the same way. External maintainers and contributors are welcomed.

The Moby project is not intended as a location for support or feature requests for Docker products, but as a place for contributors to work on open source code, fix bugs, and make the code more useful. The releases are supported by the maintainers, community and users, on a best efforts basis only, and are not intended for customers who want enterprise or commercial support; Docker EE is the appropriate product for these use cases.


Legal

Brought to you courtesy of our legal counsel. For more context, please see the NOTICE document in this repo.

Use and transfer of Moby may be subject to certain restrictions by the United States and other governments.

It is your responsibility to ensure that your use and/or transfer does not violate applicable laws.

For more information, please see https://www.bis.doc.gov

Licensing

Moby is licensed under the Apache License, Version 2.0. See LICENSE for the full license text.