Merge lp:~ted/ubuntu-app-launch/cgmanager-custom-context into lp:ubuntu-app-launch/15.04

Proposed by Ted Gould
Status: Merged
Approved by: Charles Kerr
Approved revision: 186
Merged at revision: 183
Proposed branch: lp:~ted/ubuntu-app-launch/cgmanager-custom-context
Merge into: lp:ubuntu-app-launch/15.04
Diff against target: 141 lines (+58/-8)
4 files modified
cgroup-reap-all.c (+1/-1)
helpers-shared.c (+53/-4)
helpers.h (+1/-0)
libubuntu-app-launch/ubuntu-app-launch.c (+3/-3)
To merge this branch: bzr merge lp:~ted/ubuntu-app-launch/cgmanager-custom-context
Reviewer Review Type Date Requested Status
Charles Kerr (community) Approve
PS Jenkins bot (community) continuous-integration Approve
Review via email: mp+242378@code.launchpad.net

Commit message

Create a custom GMainContext when waiting on the CGManager DBus connection.

To post a comment you must log in.
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Charles Kerr (charlesk) wrote :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'cgroup-reap-all.c'
2--- cgroup-reap-all.c 2014-08-26 19:52:27 +0000
3+++ cgroup-reap-all.c 2014-11-21 14:41:38 +0000
4@@ -59,7 +59,7 @@
5 g_list_free(pidlist);
6 }
7
8- g_clear_object(&cgmanager);
9+ cgroup_manager_unref(cgmanager);
10
11 return 0;
12 }
13
14=== modified file 'helpers-shared.c'
15--- helpers-shared.c 2014-11-18 21:10:34 +0000
16+++ helpers-shared.c 2014-11-21 14:41:38 +0000
17@@ -171,14 +171,21 @@
18 GDBusConnection *
19 cgroup_manager_connection (void)
20 {
21+ gboolean use_session_bus = g_getenv("UBUNTU_APP_LAUNCH_CG_MANAGER_SESSION_BUS") != NULL;
22+ GMainContext * context = g_main_context_new();
23+ g_main_context_push_thread_default(context);
24+
25 cgm_connection_t connection = {
26- .loop = g_main_loop_new(NULL, FALSE),
27+ .loop = g_main_loop_new(context, FALSE),
28 .con = NULL,
29 .cancel = g_cancellable_new()
30 };
31- guint timeout = g_timeout_add_seconds(1, cgroup_manager_connection_timeout_cb, &connection);
32+
33+ GSource * timesrc = g_timeout_source_new_seconds(1);
34+ g_source_set_callback(timesrc, cgroup_manager_connection_timeout_cb, &connection, NULL);
35+ g_source_attach(timesrc, context);
36
37- if (g_getenv("UBUNTU_APP_LAUNCH_CG_MANAGER_SESSION_BUS")) {
38+ if (use_session_bus) {
39 /* For working dbusmock */
40 g_debug("Connecting to CG Manager on session bus");
41 g_bus_get(G_BUS_TYPE_SESSION,
42@@ -197,13 +204,55 @@
43
44 g_main_loop_run(connection.loop);
45
46- g_source_remove(timeout);
47+ g_source_destroy(timesrc);
48+ g_source_unref(timesrc);
49+
50 g_main_loop_unref(connection.loop);
51 g_object_unref(connection.cancel);
52
53+ g_main_context_pop_thread_default(context);
54+
55+ if (!use_session_bus && connection.con != NULL) {
56+ g_object_set_data(G_OBJECT(connection.con), "cgmanager-context", context);
57+ } else {
58+ g_main_context_unref(context);
59+ }
60+
61 return connection.con;
62 }
63
64+/* This does a complex unref for the case that we're not using a shared
65+ pointer. In that case the initialization happens under the context that
66+ we used for the timeout, and it turns out GDBus saves that context to
67+ use for the close event. Upon the task closing it sends an idle source
68+ to that context which free's the last bit of memory. So we need the events
69+ on that context to be executed or we just leak. So what this does is force
70+ a close synchronously so that the event gets placed on the context and then
71+ frees the context to ensure that all of the events are processed. */
72+void
73+cgroup_manager_unref (GDBusConnection * cgmanager)
74+{
75+ if (cgmanager == NULL)
76+ return;
77+
78+ GMainContext * creationcontext = g_object_get_data(G_OBJECT(cgmanager), "cgmanager-context");
79+ if (creationcontext == NULL) {
80+ g_object_unref(cgmanager);
81+ return;
82+ }
83+
84+ GError * error = NULL;
85+ g_dbus_connection_close_sync(cgmanager, NULL, &error);
86+
87+ if (error != NULL) {
88+ g_warning("Unable to close CGManager Connection: %s", error->message);
89+ g_error_free(error);
90+ }
91+
92+ g_object_unref(cgmanager);
93+ g_main_context_unref(creationcontext);
94+}
95+
96 /* Get the PIDs for a particular cgroup */
97 /* We're using the base cgroup 'freezer' in this code (and
98 in the Upstart jobs). Really the actual group is meaningless
99
100=== modified file 'helpers.h'
101--- helpers.h 2014-08-08 19:50:53 +0000
102+++ helpers.h 2014-11-21 14:41:38 +0000
103@@ -49,6 +49,7 @@
104 void starting_handshake_wait (handshake_t * handshake);
105
106 GDBusConnection * cgroup_manager_connection (void);
107+void cgroup_manager_unref (GDBusConnection * cgroup_manager);
108 GList * pids_from_cgroup (GDBusConnection * cgmanager,
109 const gchar * jobname,
110 const gchar * instancename);
111
112=== modified file 'libubuntu-app-launch/ubuntu-app-launch.c'
113--- libubuntu-app-launch/ubuntu-app-launch.c 2014-09-30 02:16:15 +0000
114+++ libubuntu-app-launch/ubuntu-app-launch.c 2014-11-21 14:41:38 +0000
115@@ -1301,7 +1301,7 @@
116
117 if (is_click(appid)) {
118 GList * pids = pids_from_cgroup(cgmanager, "application-click", appid);
119- g_clear_object(&cgmanager);
120+ cgroup_manager_unref(cgmanager);
121
122 ual_tracepoint(pids_list_finished, appid, g_list_length(pids));
123 return pids;
124@@ -1309,7 +1309,7 @@
125 gchar * jobname = g_strdup_printf("%s-", appid);
126 GList * pids = pids_from_cgroup(cgmanager, "application-legacy", jobname);
127 g_free(jobname);
128- g_clear_object(&cgmanager);
129+ cgroup_manager_unref(cgmanager);
130
131 ual_tracepoint(pids_list_finished, appid, g_list_length(pids));
132 return pids;
133@@ -1340,7 +1340,7 @@
134 g_array_free(apps, TRUE);
135 g_object_unref(con);
136
137- g_clear_object(&cgmanager);
138+ cgroup_manager_unref(cgmanager);
139
140 ual_tracepoint(pids_list_finished, appid, g_list_length(pids));
141 return pids;

Subscribers

People subscribed via source and target branches