Skip to content

Commit

Permalink
fix spawning notifier
Browse files Browse the repository at this point in the history
Summary:
list targets was not spawning a notifier as it's new implementation was not calling get_stub(). this is a problem if list-targets is called before any other command. but since list-targets is normally the first command to be called i added another call there.

i have also added clearing the local_targets_file on `idb kill` as a cleanup

Reviewed By: zbigniew-malinowski

Differential Revision: D16985438

fbshipit-source-id: 933dc9f2a35fa7a50a473c87959658a6075cbe46
  • Loading branch information
zeyadsalloum authored and facebook-github-bot committed Aug 23, 2019
1 parent 08390e7 commit 978aaef
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
28 changes: 16 additions & 12 deletions idb/client/grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,15 +182,17 @@ def __init__(
self.direct_companion_manager = DirectCompanionManager(logger=self.logger)
self.local_targets_manager = LocalTargetsManager(logger=self.logger)
self.companion_info: Optional[CompanionInfo] = None
self.companion_spawner: Optional[CompanionSpawner] = None

@asynccontextmanager
async def get_stub(self) -> CompanionServiceStub:
if platform == "darwin" and not self.companion_spawner:
self.companion_spawner = CompanionSpawner(
async def spawn_notifier(self) -> None:
if platform == "darwin":
companion_spawner = CompanionSpawner(
companion_path="idb_companion", logger=self.logger
)
await self.companion_spawner.spawn_notifier()
await companion_spawner.spawn_notifier()

@asynccontextmanager
async def get_stub(self) -> CompanionServiceStub:
await self.spawn_notifier()
channel: Optional[Channel] = None
try:
try:
Expand Down Expand Up @@ -218,14 +220,14 @@ async def get_stub(self) -> CompanionServiceStub:
channel.close()

async def spawn_companion(self, target_udid: str) -> Optional[CompanionInfo]:
if (
self.companion_spawner
and self.local_targets_manager.is_local_target_available(
target_udid=target_udid
)
if self.local_targets_manager.is_local_target_available(
target_udid=target_udid
):
companion_spawner = CompanionSpawner(
companion_path="idb_companion", logger=self.logger
)
self.logger.info(f"will attempt to spawn a companion for {target_udid}")
port = await self.companion_spawner.spawn_companion(target_udid=target_udid)
port = await companion_spawner.spawn_companion(target_udid=target_udid)
if port:
self.logger.info(f"spawned a companion for {target_udid}")
host = "localhost"
Expand All @@ -245,6 +247,7 @@ def metadata(self) -> Dict[str, str]:

async def kill(self) -> None:
self.direct_companion_manager.clear()
self.local_targets_manager.clear()
PidSaver(logger=self.logger).kill_saved_pids()

@log_and_handle_exceptions
Expand Down Expand Up @@ -823,6 +826,7 @@ async def _companion_to_target(self, companion: CompanionInfo) -> TargetDescript

@log_and_handle_exceptions
async def list_targets(self) -> List[TargetDescription]:
await self.spawn_notifier()
companions = self.direct_companion_manager.get_companions()
local_targets = self.local_targets_manager.get_local_targets()
connected_targets = await asyncio.gather(
Expand Down
2 changes: 1 addition & 1 deletion idb/client/pid_saver.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def get_notifier_pid(self) -> int:
return self.notifier_pid

def _save(self) -> None:
with open(self.pids_file_path, "w") as pid_file:
with open(self.pids_file_path, "w+") as pid_file:
json.dump(
{"companions": self.companion_pids, "notifier": self.notifier_pid},
pid_file,
Expand Down
5 changes: 5 additions & 0 deletions idb/common/local_targets_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,8 @@ def is_local_target_available(self, target_udid: str) -> bool:
target for target in self.local_targets if target.udid == target_udid
]
return len(targets) > 0

def clear(self) -> None:
with open(self.local_targets_file, "w") as f:
json.dump([], f)
f.flush()

0 comments on commit 978aaef

Please sign in to comment.