fix(idle): track portal Response signal to avoid close errors on completed requests
- commit
- 246c06857649948507c0fe2bc761883b3966a952
- author
- bnema · · 2025-12-25 11:45 UTC
- parent
- 351fdaf
- signature
- GPG Unverified
2B82207D370127A0The key is unknown
1 file · +79 -17
M internal/infrastructure/idle/portal_inhibitor.go +79 -17
| @@ -30,11 +30,12 @@ var _ port.IdleInhibitor = (*PortalInhibitor)(nil) | |||
| 30 | 30 | // PortalInhibitor implements idle inhibition using XDG Desktop Portal. | |
| 31 | 31 | // This works on Wayland with any compositor (GNOME, KDE, sway, hyprland, etc.). | |
| 32 | 32 | type PortalInhibitor struct { | |
| 33 | - | conn *dbus.Conn | |
| 34 | - | requestPath dbus.ObjectPath // Active inhibit request handle | |
| 35 | - | refcount int | |
| 36 | - | supported bool | |
| 37 | - | mu sync.Mutex | |
| 33 | + | conn *dbus.Conn | |
| 34 | + | requestPath dbus.ObjectPath // Active inhibit request handle | |
| 35 | + | refcount int | |
| 36 | + | supported bool | |
| 37 | + | requestComplete bool // True if the portal sent a Response signal (request no longer exists) | |
| 38 | + | mu sync.Mutex | |
| 38 | 39 | } | |
| 39 | 40 | ||
| 40 | 41 | // NewPortalInhibitor creates a new portal-based idle inhibitor. | |
| @@ -113,6 +114,12 @@ func (p *PortalInhibitor) Inhibit(ctx context.Context, reason string) error { | |||
| 113 | 114 | } | |
| 114 | 115 | ||
| 115 | 116 | p.requestPath = handlePath | |
| 117 | + | p.requestComplete = false | |
| 118 | + | ||
| 119 | + | // Subscribe to the Response signal to know when the inhibition ends | |
| 120 | + | // Some portals complete the request immediately with a Response signal | |
| 121 | + | go p.watchForResponse(ctx, handlePath) | |
| 122 | + | ||
| 116 | 123 | log.Info(). | |
| 117 | 124 | Str("handle", string(handlePath)). | |
| 118 | 125 | Str("reason", reason). | |
| @@ -121,6 +128,59 @@ func (p *PortalInhibitor) Inhibit(ctx context.Context, reason string) error { | |||
| 121 | 128 | return nil | |
| 122 | 129 | } | |
| 123 | 130 | ||
| 131 | + | // watchForResponse monitors for the Response signal on the request object. | |
| 132 | + | // Some portals (particularly GNOME) complete the Inhibit request immediately, | |
| 133 | + | // which removes the Request object. We need to track this to avoid calling | |
| 134 | + | // Close on a non-existent object. | |
| 135 | + | func (p *PortalInhibitor) watchForResponse(ctx context.Context, handlePath dbus.ObjectPath) { | |
| 136 | + | log := logging.FromContext(ctx) | |
| 137 | + | ||
| 138 | + | if p.conn == nil { | |
| 139 | + | return | |
| 140 | + | } | |
| 141 | + | ||
| 142 | + | // Add match rule for the Response signal on this specific request | |
| 143 | + | matchRule := fmt.Sprintf( | |
| 144 | + | "type='signal',interface='%s',member='Response',path='%s'", | |
| 145 | + | requestIface, handlePath, | |
| 146 | + | ) | |
| 147 | + | ||
| 148 | + | if err := p.conn.BusObject().Call("org.freedesktop.DBus.AddMatch", 0, matchRule).Err; err != nil { | |
| 149 | + | log.Debug().Err(err).Msg("idle inhibitor: failed to add signal match") | |
| 150 | + | return | |
| 151 | + | } | |
| 152 | + | ||
| 153 | + | // Create a channel to receive signals | |
| 154 | + | signals := make(chan *dbus.Signal, 1) | |
| 155 | + | p.conn.Signal(signals) | |
| 156 | + | ||
| 157 | + | defer func() { | |
| 158 | + | p.conn.RemoveSignal(signals) | |
| 159 | + | _ = p.conn.BusObject().Call("org.freedesktop.DBus.RemoveMatch", 0, matchRule).Err | |
| 160 | + | }() | |
| 161 | + | ||
| 162 | + | // Wait for either a Response signal or context cancellation | |
| 163 | + | for { | |
| 164 | + | select { | |
| 165 | + | case sig := <-signals: | |
| 166 | + | if sig == nil { | |
| 167 | + | return | |
| 168 | + | } | |
| 169 | + | if sig.Path == handlePath && sig.Name == requestIface+".Response" { | |
| 170 | + | p.mu.Lock() | |
| 171 | + | p.requestComplete = true | |
| 172 | + | p.mu.Unlock() | |
| 173 | + | log.Debug(). | |
| 174 | + | Str("handle", string(handlePath)). | |
| 175 | + | Msg("idle inhibitor: request completed by portal") | |
| 176 | + | return | |
| 177 | + | } | |
| 178 | + | case <-ctx.Done(): | |
| 179 | + | return | |
| 180 | + | } | |
| 181 | + | } | |
| 182 | + | } | |
| 183 | + | ||
| 124 | 184 | // Uninhibit decrements the refcount. When zero, releases inhibition. | |
| 125 | 185 | func (p *PortalInhibitor) Uninhibit(ctx context.Context) error { | |
| 126 | 186 | log := logging.FromContext(ctx) | |
| @@ -145,17 +205,19 @@ func (p *PortalInhibitor) Uninhibit(ctx context.Context) error { | |||
| 145 | 205 | return nil | |
| 146 | 206 | } | |
| 147 | 207 | ||
| 208 | + | // If the portal already completed the request with a Response signal, | |
| 209 | + | // the Request object no longer exists - don't try to close it | |
| 210 | + | if p.requestComplete { | |
| 211 | + | log.Info().Msg("idle inhibitor: deactivated (completed by portal)") | |
| 212 | + | p.requestPath = "" | |
| 213 | + | return nil | |
| 214 | + | } | |
| 215 | + | ||
| 148 | 216 | // Close the request to release inhibition | |
| 149 | 217 | obj := p.conn.Object(portalDest, p.requestPath) | |
| 150 | - | err := obj.Call(requestIface+".Close", 0).Err | |
| 151 | - | ||
| 152 | - | if err != nil { | |
| 153 | - | log.Warn().Err(err).Msg("idle inhibitor: failed to close request") | |
| 154 | - | // Don't return error - the request may have already been closed | |
| 155 | - | } else { | |
| 156 | - | log.Info().Msg("idle inhibitor: deactivated") | |
| 157 | - | } | |
| 218 | + | _ = obj.Call(requestIface+".Close", 0).Err | |
| 158 | 219 | ||
| 220 | + | log.Info().Msg("idle inhibitor: deactivated") | |
| 159 | 221 | p.requestPath = "" | |
| 160 | 222 | return nil | |
| 161 | 223 | } | |
| @@ -172,13 +234,13 @@ func (p *PortalInhibitor) Close() error { | |||
| 172 | 234 | p.mu.Lock() | |
| 173 | 235 | defer p.mu.Unlock() | |
| 174 | 236 | ||
| 175 | - | // Release any active inhibition | |
| 176 | - | if p.conn != nil && p.requestPath != "" { | |
| 237 | + | // Release any active inhibition (only if not already completed by portal) | |
| 238 | + | if p.conn != nil && p.requestPath != "" && !p.requestComplete { | |
| 177 | 239 | obj := p.conn.Object(portalDest, p.requestPath) | |
| 178 | 240 | _ = obj.Call(requestIface+".Close", 0).Err | |
| 179 | - | p.requestPath = "" | |
| 180 | 241 | } | |
| 181 | - | ||
| 242 | + | p.requestPath = "" | |
| 243 | + | p.requestComplete = false | |
| 182 | 244 | p.refcount = 0 | |
| 183 | 245 | ||
| 184 | 246 | if p.conn != nil { | |
| @@ -30,11 +30,12 @@ var _ port.IdleInhibitor = (*PortalInhibitor)(nil) | |||||
| 30 | // PortalInhibitor implements idle inhibition using XDG Desktop Portal. | 30 | // PortalInhibitor implements idle inhibition using XDG Desktop Portal. | ||
| 31 | // This works on Wayland with any compositor (GNOME, KDE, sway, hyprland, etc.). | 31 | // This works on Wayland with any compositor (GNOME, KDE, sway, hyprland, etc.). | ||
| 32 | type PortalInhibitor struct { | 32 | type PortalInhibitor struct { | ||
| 33 | - | conn *dbus.Conn | 33 | + | conn *dbus.Conn |
| 34 | - | requestPath dbus.ObjectPath // Active inhibit request handle | 34 | + | requestPath dbus.ObjectPath // Active inhibit request handle |
| 35 | - | refcount int | 35 | + | refcount int |
| 36 | - | supported bool | 36 | + | supported bool |
| 37 | - | mu sync.Mutex | 37 | + | requestComplete bool // True if the portal sent a Response signal (request no longer exists) |
| 38 | + | mu sync.Mutex | |||
| 38 | } | 39 | } | ||
| 39 | 40 | ||||
| 40 | // NewPortalInhibitor creates a new portal-based idle inhibitor. | 41 | // NewPortalInhibitor creates a new portal-based idle inhibitor. | ||
| @@ -113,6 +114,12 @@ func (p *PortalInhibitor) Inhibit(ctx context.Context, reason string) error { | |||||
| 113 | } | 114 | } | ||
| 114 | 115 | ||||
| 115 | p.requestPath = handlePath | 116 | p.requestPath = handlePath | ||
| 117 | + | p.requestComplete = false | |||
| 118 | + | ||||
| 119 | + | // Subscribe to the Response signal to know when the inhibition ends | |||
| 120 | + | // Some portals complete the request immediately with a Response signal | |||
| 121 | + | go p.watchForResponse(ctx, handlePath) | |||
| 122 | + | ||||
| 116 | log.Info(). | 123 | log.Info(). | ||
| 117 | Str("handle", string(handlePath)). | 124 | Str("handle", string(handlePath)). | ||
| 118 | Str("reason", reason). | 125 | Str("reason", reason). | ||
| @@ -121,6 +128,59 @@ func (p *PortalInhibitor) Inhibit(ctx context.Context, reason string) error { | |||||
| 121 | return nil | 128 | return nil | ||
| 122 | } | 129 | } | ||
| 123 | 130 | ||||
| 131 | + | // watchForResponse monitors for the Response signal on the request object. | |||
| 132 | + | // Some portals (particularly GNOME) complete the Inhibit request immediately, | |||
| 133 | + | // which removes the Request object. We need to track this to avoid calling | |||
| 134 | + | // Close on a non-existent object. | |||
| 135 | + | func (p *PortalInhibitor) watchForResponse(ctx context.Context, handlePath dbus.ObjectPath) { | |||
| 136 | + | log := logging.FromContext(ctx) | |||
| 137 | + | ||||
| 138 | + | if p.conn == nil { | |||
| 139 | + | return | |||
| 140 | + | } | |||
| 141 | + | ||||
| 142 | + | // Add match rule for the Response signal on this specific request | |||
| 143 | + | matchRule := fmt.Sprintf( | |||
| 144 | + | "type='signal',interface='%s',member='Response',path='%s'", | |||
| 145 | + | requestIface, handlePath, | |||
| 146 | + | ) | |||
| 147 | + | ||||
| 148 | + | if err := p.conn.BusObject().Call("org.freedesktop.DBus.AddMatch", 0, matchRule).Err; err != nil { | |||
| 149 | + | log.Debug().Err(err).Msg("idle inhibitor: failed to add signal match") | |||
| 150 | + | return | |||
| 151 | + | } | |||
| 152 | + | ||||
| 153 | + | // Create a channel to receive signals | |||
| 154 | + | signals := make(chan *dbus.Signal, 1) | |||
| 155 | + | p.conn.Signal(signals) | |||
| 156 | + | ||||
| 157 | + | defer func() { | |||
| 158 | + | p.conn.RemoveSignal(signals) | |||
| 159 | + | _ = p.conn.BusObject().Call("org.freedesktop.DBus.RemoveMatch", 0, matchRule).Err | |||
| 160 | + | }() | |||
| 161 | + | ||||
| 162 | + | // Wait for either a Response signal or context cancellation | |||
| 163 | + | for { | |||
| 164 | + | select { | |||
| 165 | + | case sig := <-signals: | |||
| 166 | + | if sig == nil { | |||
| 167 | + | return | |||
| 168 | + | } | |||
| 169 | + | if sig.Path == handlePath && sig.Name == requestIface+".Response" { | |||
| 170 | + | p.mu.Lock() | |||
| 171 | + | p.requestComplete = true | |||
| 172 | + | p.mu.Unlock() | |||
| 173 | + | log.Debug(). | |||
| 174 | + | Str("handle", string(handlePath)). | |||
| 175 | + | Msg("idle inhibitor: request completed by portal") | |||
| 176 | + | return | |||
| 177 | + | } | |||
| 178 | + | case <-ctx.Done(): | |||
| 179 | + | return | |||
| 180 | + | } | |||
| 181 | + | } | |||
| 182 | + | } | |||
| 183 | + | ||||
| 124 | // Uninhibit decrements the refcount. When zero, releases inhibition. | 184 | // Uninhibit decrements the refcount. When zero, releases inhibition. | ||
| 125 | func (p *PortalInhibitor) Uninhibit(ctx context.Context) error { | 185 | func (p *PortalInhibitor) Uninhibit(ctx context.Context) error { | ||
| 126 | log := logging.FromContext(ctx) | 186 | log := logging.FromContext(ctx) | ||
| @@ -145,17 +205,19 @@ func (p *PortalInhibitor) Uninhibit(ctx context.Context) error { | |||||
| 145 | return nil | 205 | return nil | ||
| 146 | } | 206 | } | ||
| 147 | 207 | ||||
| 208 | + | // If the portal already completed the request with a Response signal, | |||
| 209 | + | // the Request object no longer exists - don't try to close it | |||
| 210 | + | if p.requestComplete { | |||
| 211 | + | log.Info().Msg("idle inhibitor: deactivated (completed by portal)") | |||
| 212 | + | p.requestPath = "" | |||
| 213 | + | return nil | |||
| 214 | + | } | |||
| 215 | + | ||||
| 148 | // Close the request to release inhibition | 216 | // Close the request to release inhibition | ||
| 149 | obj := p.conn.Object(portalDest, p.requestPath) | 217 | obj := p.conn.Object(portalDest, p.requestPath) | ||
| 150 | - | err := obj.Call(requestIface+".Close", 0).Err | 218 | + | _ = obj.Call(requestIface+".Close", 0).Err |
| 151 | - | ||||
| 152 | - | if err != nil { | |||
| 153 | - | log.Warn().Err(err).Msg("idle inhibitor: failed to close request") | |||
| 154 | - | // Don't return error - the request may have already been closed | |||
| 155 | - | } else { | |||
| 156 | - | log.Info().Msg("idle inhibitor: deactivated") | |||
| 157 | - | } | |||
| 158 | 219 | ||||
| 220 | + | log.Info().Msg("idle inhibitor: deactivated") | |||
| 159 | p.requestPath = "" | 221 | p.requestPath = "" | ||
| 160 | return nil | 222 | return nil | ||
| 161 | } | 223 | } | ||
| @@ -172,13 +234,13 @@ func (p *PortalInhibitor) Close() error { | |||||
| 172 | p.mu.Lock() | 234 | p.mu.Lock() | ||
| 173 | defer p.mu.Unlock() | 235 | defer p.mu.Unlock() | ||
| 174 | 236 | ||||
| 175 | - | // Release any active inhibition | 237 | + | // Release any active inhibition (only if not already completed by portal) |
| 176 | - | if p.conn != nil && p.requestPath != "" { | 238 | + | if p.conn != nil && p.requestPath != "" && !p.requestComplete { |
| 177 | obj := p.conn.Object(portalDest, p.requestPath) | 239 | obj := p.conn.Object(portalDest, p.requestPath) | ||
| 178 | _ = obj.Call(requestIface+".Close", 0).Err | 240 | _ = obj.Call(requestIface+".Close", 0).Err | ||
| 179 | - | p.requestPath = "" | |||
| 180 | } | 241 | } | ||
| 181 | - | 242 | + | p.requestPath = "" | |
| 243 | + | p.requestComplete = false | |||
| 182 | p.refcount = 0 | 244 | p.refcount = 0 | ||
| 183 | 245 | ||||
| 184 | if p.conn != nil { | 246 | if p.conn != nil { | ||