{"record":{"id":"45a1afb6b934d313","repo":"hashicorp/nomad","slug":"lost-after-cannot-be-a-negative-duration","errorCode":null,"errorMessage":"lost_after cannot be a negative duration","messagePattern":"lost_after cannot be a negative duration","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"nomad/structs/group.go","lineNumber":27,"sourceCode":"\t\"time\"\n\n\t\"github.com/hashicorp/go-multierror\"\n)\n\nconst (\n\t// ReconcileOption is used to specify the behavior of the reconciliation process\n\t// between the original allocations and the replacements when a previously\n\t// disconnected client comes back online.\n\tReconcileOptionKeepOriginal    = \"keep_original\"\n\tReconcileOptionKeepReplacement = \"keep_replacement\"\n\tReconcileOptionBestScore       = \"best_score\"\n\tReconcileOptionLongestRunning  = \"longest_running\"\n)\n\nvar (\n\t// Disconnect strategy validation errors\n\terrStopAndLost         = errors.New(\"Disconnect cannot be configured with both lost_after and stop_after\")\n\terrNegativeLostAfter   = errors.New(\"lost_after cannot be a negative duration\")\n\terrNegativeStopAfter   = errors.New(\"stop_after cannot be a negative duration\")\n\terrStopAfterNonService = errors.New(\"stop_after can only be used with service or batch job types\")\n\terrInvalidReconcile    = errors.New(\"reconcile option is invalid\")\n)\n\nfunc NewDefaultDisconnectStrategy() *DisconnectStrategy {\n\treturn &DisconnectStrategy{\n\t\tReplace:   new(true),\n\t\tReconcile: ReconcileOptionBestScore,\n\t}\n}\n\n// Disconnect strategy defines how both clients and server should behave in case of\n// disconnection between them.\ntype DisconnectStrategy struct {\n\t// Defines for how long the server will consider the unresponsive node as\n\t// disconnected but alive instead of lost.\n\tLostAfter time.Duration `mapstructure:\"lost_after\" hcl:\"lost_after,optional\"`","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/hashicorp/nomad/blob/482b49bf1aec006f089bcfc7e632d8f6ac303e5e/nomad/structs/group.go#L9-L45","documentation":"errNegativeLostAfter is a disconnect-strategy validation error: lost_after must be zero or a positive duration. A negative value makes no temporal sense (it would mark the client lost before it disconnects), so Validate rejects the job.","triggerScenarios":"Submitting a job whose group disconnect block has lost_after set to a negative duration, checked at group.go:84 in DisconnectStrategy.Validate.","commonSituations":"HCL negative durations like lost_after = \"-5m\" from templating bugs or manual typos; variables interpolated from data that can go negative (e.g. difference of timestamps); copy-paste errors.","solutions":["Set lost_after to a positive duration (e.g. \"2h\") or remove it to use the default","Clamp computed/templated durations to >= 0 before rendering the job file","Run 'nomad job validate' to catch the negative value before submission"],"exampleFix":"// before\ndisconnect {\n  lost_after = \"-5m\"\n}\n// after\ndisconnect {\n  lost_after = \"5m\"\n}","handlingStrategy":"validation","validationCode":"if ds := grp.Disconnect; ds != nil && ds.LostAfter < 0 {\n    return fmt.Errorf(\"disconnect: lost_after must be >= 0, got %s\", ds.LostAfter)\n}","typeGuard":"func lostAfterValid(ds *api.DisconnectStrategy) bool {\n    return ds == nil || ds.LostAfter >= 0\n}","tryCatchPattern":"err := client.Jobs().Validate(job, nil, nil)\nif err != nil && strings.Contains(err.Error(), \"lost_after cannot be a negative duration\") {\n    return fmt.Errorf(\"fix lost_after to a positive duration: %w\", err)\n}","preventionTips":["Never emit negative durations from templates; clamp computed values with max(0, d)","Use HCL variables with validation blocks to reject negative durations","Run 'nomad job validate' as a pre-submit check"],"tags":["nomad","job-spec","validation","disconnect"],"backgroundTag":"invalid-duration-config","analyzedSha":"482b49bf1aec006f089bcfc7e632d8f6ac303e5e","analyzedAt":"2026-09-04T07:54:14.808Z","contentChangedAt":"2026-09-04T07:54:14.808Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}