Can't activate all Cycles Render Devices from script on first try

Hi folks!

I posted it as a bug T61838, but now I think this is a better place for such discussion.
Don’t know is this a bug or not, got script from here: T54099. Can’t activate all Cycles Render Devices from script on first try.

Add script to Text Editor and run it.
Open file menu in topbar -> Press Load DotBow Config -> Check Cycles Render Devices in settings, only GPU is selected.
Repeat steps and check Cycles Render Devices again - all devices active.

import bpy

class TOPBAR_OT_load_dotbow_config(bpy.types.Operator):
    bl_idname = "wm.load_dotbow_config"
    bl_label = "Load DotBow Config"

    def execute(self, context):
        # bpy.ops.wm.read_factory_settings()

        scene = bpy.context.scene
        scene.cycles.device = 'GPU'

        prefs = bpy.context.preferences
        cprefs = prefs.addons['cycles'].preferences

        # Attempt to set GPU device types if available
        for compute_device_type in ('CUDA', 'OPENCL', 'NONE'):
            try:
                cprefs.compute_device_type = compute_device_type
                break
            except TypeError:
                pass

        # Enable all CPU and GPU devices
        for device in cprefs.devices:
            device.use = True

        # bpy.ops.wm.save_homefile()

        return {'FINISHED'}


def menu_func(self, context):
    layout = self.layout
    layout.operator(TOPBAR_OT_load_dotbow_config.bl_idname)


classes = (
    TOPBAR_OT_load_dotbow_config,
)


def register():
    for cls in classes:
        bpy.utils.register_class(cls)
    bpy.types.TOPBAR_MT_file.prepend(menu_func)


def unregister():
    for cls in reversed(classes):
        bpy.utils.unregister_class(cls)
    bpy.types.TOPBAR_MT_file.remove(menu_func)


if __name__ == "__main__":
    register()