Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Protect object map from concurrent access #340

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions default_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,29 @@ type defaultHandler struct {
defaultIntf map[string]*exportedIntf
}

func (h *defaultHandler) PathExists(path ObjectPath) bool {
_, ok := h.objects[path]
return ok
func (h *defaultHandler) GetExportedObject(path ObjectPath) (*exportedObj, bool) {
h.RLock()
defer h.RUnlock()
obj, ok := h.objects[path]
return obj, ok
Comment on lines +44 to +45

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
obj, ok := h.objects[path]
return obj, ok
return h.objects[path]

}

// GetOrAddExportedObject returns an exportedObj for an specific ObjectPath
// A new exportedObj is created if none existed for ObjectPath
func (h *defaultHandler) GetOrAddExportedObject(path ObjectPath) *exportedObj {
h.RLock()
defer h.RUnlock()
Comment on lines +51 to +52
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be a write lock then. The race detector picked this up as well: https://github.com/godbus/dbus/actions/runs/4797626686/jobs/9033883115?pr=340#step:6:243

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So true. Fixed!

obj, ok := h.objects[path]
if !ok {
obj = newExportedObject()
h.objects[path] = obj
}
return obj
}

func (h *defaultHandler) introspectPath(path ObjectPath) string {
h.RLock()
defer h.RUnlock()
subpath := make(map[string]struct{})
var xml bytes.Buffer
xml.WriteString("<node>")
Expand All @@ -65,8 +82,8 @@ func (h *defaultHandler) introspectPath(path ObjectPath) string {
}

func (h *defaultHandler) LookupObject(path ObjectPath) (ServerObject, bool) {
h.RLock()
defer h.RUnlock()
h.Lock()
defer h.Unlock()
object, ok := h.objects[path]
if ok {
return object, ok
Expand Down
16 changes: 6 additions & 10 deletions export.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,8 +363,7 @@ func (conn *Conn) exportMethodTable(methods map[string]interface{}, path ObjectP
}

func (conn *Conn) unexport(h *defaultHandler, path ObjectPath, iface string) error {
if h.PathExists(path) {
obj := h.objects[path]
if obj, ok := h.GetExportedObject(path); ok {
obj.DeleteInterface(iface)
if len(obj.interfaces) == 0 {
h.DeleteObject(path)
Expand All @@ -391,21 +390,18 @@ func (conn *Conn) export(methods map[string]reflect.Value, path ObjectPath, ifac
return conn.unexport(h, path, iface)
}

// If this is the first handler for this path, make a new map to hold all
// handlers for this path.
if !h.PathExists(path) {
h.AddObject(path, newExportedObject())
}

// Get all Methods that should be exported
// Get all Methods that should be exported
exportedMethods := make(map[string]Method)
for name, method := range methods {
exportedMethods[name] = exportedMethod{method}
}

// Get a handler for the path, either an existing or a new fresh one
obj := h.GetOrAddExportedObject(path)

// Finally, save this handler
obj := h.objects[path]
obj.AddInterface(iface, newExportedIntf(exportedMethods, includeSubtree))

return nil
}

Expand Down