{"record":{"id":"dcf633c0a8a9813f","repo":"puppetlabs/puppet","slug":"failed-to-set-file-attributes","errorCode":null,"errorMessage":"Failed to set file attributes","messagePattern":"Failed to set file attributes","errorType":"exception","errorClass":"Puppet::Util::Windows::Error","httpStatus":null,"severity":"error","filePath":"lib/puppet/util/windows/file.rb","lineNumber":120,"sourceCode":"\n    if (oldattrs | flags) != oldattrs\n      set_attributes(path, oldattrs | flags)\n    end\n  end\n  module_function :add_attributes\n\n  def remove_attributes(path, flags)\n    oldattrs = get_attributes(path)\n\n    if (oldattrs & ~flags) != oldattrs\n      set_attributes(path, oldattrs & ~flags)\n    end\n  end\n  module_function :remove_attributes\n\n  def set_attributes(path, flags)\n    success = SetFileAttributesW(wide_string(path), flags) != FFI::WIN32_FALSE\n    raise Puppet::Util::Windows::Error, _(\"Failed to set file attributes\") unless success\n\n    success\n  end\n  module_function :set_attributes\n\n  # define INVALID_HANDLE_VALUE ((HANDLE)(LONG_PTR)-1)\n  INVALID_HANDLE_VALUE = FFI::Pointer.new(-1).address\n  def self.create_file(file_name, desired_access, share_mode, security_attributes,\n                       creation_disposition, flags_and_attributes, template_file_handle)\n\n    result = CreateFileW(wide_string(file_name.to_s),\n                         desired_access, share_mode, security_attributes, creation_disposition,\n                         flags_and_attributes, template_file_handle)\n\n    return result unless result == INVALID_HANDLE_VALUE\n\n    raise Puppet::Util::Windows::Error, \"CreateFile(#{file_name}, #{desired_access.to_s(8)}, #{share_mode.to_s(8)}, \" \\\n                                        \"#{security_attributes}, #{creation_disposition.to_s(8)}, \" \\","sourceCodeStart":102,"sourceCodeEnd":138,"githubUrl":"https://github.com/puppetlabs/puppet/blob/e227c27540975c25aa22d533a52424a9d2fc886a/lib/puppet/util/windows/file.rb#L102-L138","documentation":"Puppet::Util::Windows::File.set_attributes calls SetFileAttributesW and raises Puppet::Util::Windows::Error when it returns FALSE. It is used by add_attributes/remove_attributes (e.g. toggling FILE_ATTRIBUTE_READONLY around file operations). The appended Win32 message and e.code give the cause: invalid path, access denied, or the file being locked in an incompatible state.","triggerScenarios":"set_attributes(path, flags) when the path no longer exists (2/3), the caller cannot write the file's metadata (5), or another handle has the file open with sharing restrictions (32). Also triggered by passing a combined flags value where some bits conflict with the file's current state.","commonSituations":"Puppet clearing the read-only bit before deleting or replacing a file while antivirus or a backup job holds it; files on read-only network shares; non-admin agents touching system files; files already deleted by a previous resource in the same run.","solutions":["Confirm the file still exists at the moment of the call (get_attributes(path, false))","Map e.code: 5 = elevate or fix ACLs, 32 = retry after the lock clears, 2/3 = fix the path","Sequence operations so attributes are changed immediately before the dependent action, minimizing the race window","Schedule AV exclusions for managed directories if code 32 recurs"],"exampleFix":"// before\nPuppet::Util::Windows::File.set_attributes(path, new_flags)\n\n// after\nbegin\n  Puppet::Util::Windows::File.set_attributes(path, new_flags)\nrescue Puppet::Util::Windows::Error => e\n  retry if e.code == 32 && (retry_count += 1) < 3\n  raise\nend","handlingStrategy":"try-catch","validationCode":"exists = Puppet::Util::Windows::File.get_attributes(path, false) != Puppet::Util::Windows::File::INVALID_FILE_ATTRIBUTES\nraise ArgumentError, \"#{path} gone\" unless exists","typeGuard":null,"tryCatchPattern":"begin\n  Puppet::Util::Windows::File.set_attributes(path, flags)\nrescue Puppet::Util::Windows::Error => e\n  retry if e.code == 32 && (tries ||= 0) < 3  # sharing violation is transient\n  raise\nend","preventionTips":["Change attributes immediately before the dependent delete/replace to shrink the race window","Do not retry code 5 (access denied) - fix rights or elevation instead","Add AV exclusions for managed trees if attribute flips keep hitting locks"],"tags":["windows","file-system","win32-api","ffi","file-attributes","puppet"],"backgroundTag":"win32-file-attributes-failed","analyzedSha":"e227c27540975c25aa22d533a52424a9d2fc886a","analyzedAt":"2026-08-21T20:49:46.650Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}