ECS starting agent fails with: Could not unmarshal state
Summary
After enabling docker debugging as per this article: http://docs.aws.amazon.com/AmazonECS/latest/developerguide/docker-debug-mode.html
and running :
sudo service docker restart sudo start ecs
the ECS agent failed to start with :
[CRITICAL] Error loading previously saved state: Could not unmarshal state; incomplete save. There was no task for arn arn:aws:ecs:us-west-2:888922156537:task/bd886aa4-d3d3-4cbd-8588-6f02b793d4a9
Expected Behavior
The agent and services container should start.
Observed Behavior
Agent container fails to start
Environment Details
$ docker info Containers: 4 Running: 0 Paused: 0 Stopped: 4 Images: 4 Server Version: 17.03.2-ce Storage Driver: devicemapper Pool Name: docker-docker--pool Pool Blocksize: 524.3 kB Base Device Size: 10.74 GB Backing Filesystem: ext4 Data file: Metadata file: Data Space Used: 2.659 GB Data Space Total: 23.33 GB Data Space Available: 20.67 GB Metadata Space Used: 1.241 MB Metadata Space Total: 25.17 MB Metadata Space Available: 23.92 MB Thin Pool Minimum Free Space: 2.333 GB Udev Sync Supported: true Deferred Removal Enabled: true Deferred Deletion Enabled: true Deferred Deleted Device Count: 0 Library Version: 1.02.135-RHEL7 (2016-11-16) Logging Driver: json-file Cgroup Driver: cgroupfs Plugins: Volume: local Network: bridge host macvlan null overlay Swarm: inactive Runtimes: runc Default Runtime: runc Init Binary: docker-init containerd version: 4ab9917febca54791c5f071a9d1f404867857fcc runc version: 54296cf40ad8143b62dbcaa1d90e520a2136ddfe init version: 949e6fa Security Options: seccomp Profile: default Kernel Version: 4.9.43-17.39.amzn1.x86_64 Operating System: Amazon Linux AMI 2017.03 OSType: linux Architecture: x86_64 CPUs: 2 Total Memory: 3.86 GiB Name: ip-172-31-10-150 ID: 2WGQ:CIAI:RRSI:3OB4:JFFI:FHYO:HTT7:KTH2:7AUS:IYZZ:VTBK:77RJ Docker Root Dir: /var/lib/docker Debug Mode (client): false Debug Mode (server): false Registry: https://index.docker.io/v1/ Experimental: false Insecure Registries: 127.0.0.0/8 Live Restore Enabled: false
$ curl http://localhost:51678/v1/metadata curl: (7) Failed to connect to localhost port 51678: Connection refused
$ df -h Filesystem Size Used Avail Use% Mounted on /dev/xvda1 7.8G 801M 6.9G 11% / devtmpfs 2.0G 76K 2.0G 1% /dev tmpfs 2.0G 0 2.0G 0% /dev/shm
Supporting Log Snippets
2017-10-20T07:41:20Z [INFO] Loading configuration 2017-10-20T07:41:20Z [DEBUG] Environment variable empty: ECS_CONTAINER_STOP_TIMEOUT 2017-10-20T07:41:20Z [DEBUG] Environment variable empty: ECS_ENGINE_TASK_CLEANUP_WAIT_DURATION 2017-10-20T07:41:20Z [DEBUG] Environment variable empty: ECS_IMAGE_MINIMUM_CLEANUP_AGE 2017-10-20T07:41:20Z [DEBUG] Environment variable empty: ECS_IMAGE_CLEANUP_INTERVAL 2017-10-20T07:41:20Z [DEBUG] Loaded config: Cluster: cluster-1, Region: us-west-2, DataDir: /data, Checkpoint: true, AuthType: dockercfg, UpdatesEnabled: true, DisableMetrics: false, ReservedMem: 0, TaskCleanupWaitDuration: 3h0m0s, DockerStopTimeout: 30s, PauseContainerImageName: amazon/amazon-ecs-pause, PauseContainerTag: 0.1.0 2017-10-20T07:41:20Z [INFO] Loading state! module="statemanager" 2017-10-20T07:41:20Z [INFO] Event stream ContainerChange start listening... 2017-10-20T07:41:20Z [DEBUG] Could not unmarshal into actual module="statemanager" 2017-10-20T07:41:20Z [CRITICAL] Error loading previously saved state: Could not unmarshal state; incomplete save. There was no task for arn arn:aws:ecs:us-west-2:888922156537:task/bd886aa4-d3d3-4cbd-8588-6f02b793d4a9
Just FYI: after returning the debug parameter to its original value(no debugging) the problem persists. Is there a way to manually rebuild the esc agent container and restart it? Removing the container and image and then reinstalling ecs-init did not help either. docker rm ecs_agent_container docker rmi ecs_agent_image sudo yum remove ecs-init sudo yum install ecs-init && sudo start ecs
@tsonev thanks a lot for reporting this.
The agent maintains its state in a flat file and it appears as though the agent is unable to marshal this state upon startup.
To help investigate this issue, it would be extremely helpful if you could provide more details on the actions you performed that resulted in this state.
You could use the amazon-ecs-logs-collector to send the logs to sidvin at amazon dot com.
@vsiddharth ah sorry to say this but since this was one of our UAT environments it was critical that we restore it to working order asap. Thats why we scaled the cluster by spawning a new instance and drained this one.. As far as the actions I took, really there's nothing more to report, we noticed a problem with one of the three containers on that instance(mysql connection was not getting through to the RDS) and I logged in and changed the docker debug setting to -D Then restarted docker, and then ran start ecs(without stopping it first) as per the manual's description. Then I noticed that all containers were down, perhaps make sense since I restarted docker, and tried starting them. Our containers started and worked(but were useless without the ability to redeploy them via the AWS services) without an issue while the ecs kept throwing the above error no matter what. Sorry for not keeping the instance around to gather the info you requested.
I'm using a Windows 2016 Container Cluster, and have had this problem occur twice in the last few days. The first time it happened, I didn't notice anything out of the ordinary. Docker was functioning normally. I had shutdown the EC2 instance overnight, and I'm wondering if that had something to do with it.
The second time it happened, was with an EC2 instance where Docker started having problems, and was failing to start. We had to uninstall / reinstall Docker, and then I noticed the ECS Agent was reporting this same unmarshalling error.
In both cases, I deleted the ECS Agent json data file in C:\ProgramData\Amazon\ECS\data, at which point the ECS Agent starts working again, but a new ECS Container Instance is created. I marked the old container instance as draining and then deregistered it.
@davemeier Do you still have the old state file in C:\ProgramData\Amazon\ECS\data? If you have the old one, I'd appreciate it if you can email it to me at skarp at amazon.com.
@tsonev @davemeier I'd really appreciate the state file and full logs. On Linux, you can use the ECS logs collector to grab them; on Windows, you'll find them in C:\ProgramData\Amazon\ECS. Once you have those, please email them to me at skarp at amazon.com.
@samuelkarp I don't have the old json state file, but I'll email the currently working one to you along with a zip of the log files. Since it has happened twice in a few days, which is fairly frequent, I will be sure to save the json file next time it happens.
@davemeier Thanks! Log files will help, but the currently-working state file won't really tell me much (since it's already working!).
@samuelkarp We’re in luck! It appears that I did actually save off a copy of the json before deleting it, which I found when I double-checked the disk. Note that you will see lots of errors in there, as I was just starting out trying to get my container to work, and the task was failing for various reasons. Since then I have been able to get my containers all running successfully. Sending email to you now...I also have a question in there about the process that re-spawns ecs-agent when it finds that it's not running. What process does this? I had to update the agent manually, and had to resort to killing the agent and then quickly swapping in the new version of the agent. In the UI, it failed to update the agent, which is why I was going the manual route.
@davemeier Thanks! Using the state file you sent me, we were able to figure out what's going wrong here. I'll have a PR out shortly to fix this.
It looks like this is an issue specifically when you have tasks that fail to pull the containers and you restart the agent after the agent has attempted to clean up those tasks. This results in entries in the IdToTask and IdToContainer maps in the state file that have keys prefixed with "ecs-" (like "ecs-private-image-1-private-9a88bdf996e5a1f43f00"). To work around this issue until we have a fix out, you can remove those entries from the IdToTask and IdToContainer maps in the state file.
I've posted a fix in https://github.com/aws/amazon-ecs-agent/pull/1033, which will need to be reviewed before we can merge it.
This only affects v1.14.5; versions prior to this do not have the bug.
We have released ECS agent v1.15.0, which resolves this bug.
Hi @samuelkarp - I've run into this same issue. Before I upgrade the agent for our ecs ami (hoping this is the fix), is there anything I can provide to confirm the same root cause. When trying to upgrade the ecs agent on one of cluster instances via the console, have this in ecs-init.log
2017-11-09T09:36:46Z [INFO] Agent exited with code 5
2017-11-09T09:36:46Z [ERROR] agent exited with terminal exit code
and this in the ecs.agent.log.xxx:
2017-11-09T09:36:46Z [CRITICAL] Error loading previously saved state: Could not unmarshal state; incomplete save. There was no task for arn arn:aws:ecs:us-east-1:xxxxxxxx:task/xxxxxxxxxx
Thanks
@chadsmall That looks like the same issue. You can either replace your instances and launch fresh ones with the new AMI, or follow the workaround instructions here: https://github.com/aws/amazon-ecs-agent/issues/1024#issuecomment-339174265.