test_process.py 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2009, Giampaolo Rodola'. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. """Tests for psutil.Process class."""
  6. import collections
  7. import contextlib
  8. import errno
  9. import getpass
  10. import io
  11. import itertools
  12. import os
  13. import signal
  14. import socket
  15. import stat
  16. import string
  17. import subprocess
  18. import sys
  19. import textwrap
  20. import time
  21. from unittest import mock
  22. import psutil
  23. from psutil import AIX
  24. from psutil import BSD
  25. from psutil import LINUX
  26. from psutil import MACOS
  27. from psutil import NETBSD
  28. from psutil import OPENBSD
  29. from psutil import OSX
  30. from psutil import POSIX
  31. from psutil import WINDOWS
  32. from psutil._common import open_text
  33. from psutil.tests import CI_TESTING
  34. from psutil.tests import GITHUB_ACTIONS
  35. from psutil.tests import GLOBAL_TIMEOUT
  36. from psutil.tests import HAS_CPU_AFFINITY
  37. from psutil.tests import HAS_ENVIRON
  38. from psutil.tests import HAS_IONICE
  39. from psutil.tests import HAS_MEMORY_MAPS
  40. from psutil.tests import HAS_PROC_CPU_NUM
  41. from psutil.tests import HAS_PROC_IO_COUNTERS
  42. from psutil.tests import HAS_RLIMIT
  43. from psutil.tests import HAS_THREADS
  44. from psutil.tests import MACOS_11PLUS
  45. from psutil.tests import PYPY
  46. from psutil.tests import PYTHON_EXE
  47. from psutil.tests import PYTHON_EXE_ENV
  48. from psutil.tests import PsutilTestCase
  49. from psutil.tests import ThreadTask
  50. from psutil.tests import call_until
  51. from psutil.tests import copyload_shared_lib
  52. from psutil.tests import create_c_exe
  53. from psutil.tests import create_py_exe
  54. from psutil.tests import process_namespace
  55. from psutil.tests import pytest
  56. from psutil.tests import reap_children
  57. from psutil.tests import retry_on_failure
  58. from psutil.tests import sh
  59. from psutil.tests import skip_on_access_denied
  60. from psutil.tests import skip_on_not_implemented
  61. from psutil.tests import wait_for_pid
  62. # ===================================================================
  63. # --- psutil.Process class tests
  64. # ===================================================================
  65. class TestProcess(PsutilTestCase):
  66. """Tests for psutil.Process class."""
  67. def test_pid(self):
  68. p = psutil.Process()
  69. assert p.pid == os.getpid()
  70. with pytest.raises(AttributeError):
  71. p.pid = 33
  72. def test_kill(self):
  73. p = self.spawn_psproc()
  74. p.kill()
  75. code = p.wait()
  76. if WINDOWS:
  77. assert code == signal.SIGTERM
  78. else:
  79. assert code == -signal.SIGKILL
  80. self.assert_proc_gone(p)
  81. def test_terminate(self):
  82. p = self.spawn_psproc()
  83. p.terminate()
  84. code = p.wait()
  85. if WINDOWS:
  86. assert code == signal.SIGTERM
  87. else:
  88. assert code == -signal.SIGTERM
  89. self.assert_proc_gone(p)
  90. def test_send_signal(self):
  91. sig = signal.SIGKILL if POSIX else signal.SIGTERM
  92. p = self.spawn_psproc()
  93. p.send_signal(sig)
  94. code = p.wait()
  95. if WINDOWS:
  96. assert code == sig
  97. else:
  98. assert code == -sig
  99. self.assert_proc_gone(p)
  100. @pytest.mark.skipif(not POSIX, reason="not POSIX")
  101. def test_send_signal_mocked(self):
  102. sig = signal.SIGTERM
  103. p = self.spawn_psproc()
  104. with mock.patch('psutil.os.kill', side_effect=ProcessLookupError):
  105. with pytest.raises(psutil.NoSuchProcess):
  106. p.send_signal(sig)
  107. p = self.spawn_psproc()
  108. with mock.patch('psutil.os.kill', side_effect=PermissionError):
  109. with pytest.raises(psutil.AccessDenied):
  110. p.send_signal(sig)
  111. def test_wait_exited(self):
  112. # Test waitpid() + WIFEXITED -> WEXITSTATUS.
  113. # normal return, same as exit(0)
  114. cmd = [PYTHON_EXE, "-c", "pass"]
  115. p = self.spawn_psproc(cmd)
  116. code = p.wait()
  117. assert code == 0
  118. self.assert_proc_gone(p)
  119. # exit(1), implicit in case of error
  120. cmd = [PYTHON_EXE, "-c", "1 / 0"]
  121. p = self.spawn_psproc(cmd, stderr=subprocess.PIPE)
  122. code = p.wait()
  123. assert code == 1
  124. self.assert_proc_gone(p)
  125. # via sys.exit()
  126. cmd = [PYTHON_EXE, "-c", "import sys; sys.exit(5);"]
  127. p = self.spawn_psproc(cmd)
  128. code = p.wait()
  129. assert code == 5
  130. self.assert_proc_gone(p)
  131. # via os._exit()
  132. cmd = [PYTHON_EXE, "-c", "import os; os._exit(5);"]
  133. p = self.spawn_psproc(cmd)
  134. code = p.wait()
  135. assert code == 5
  136. self.assert_proc_gone(p)
  137. @pytest.mark.skipif(NETBSD, reason="fails on NETBSD")
  138. def test_wait_stopped(self):
  139. p = self.spawn_psproc()
  140. if POSIX:
  141. # Test waitpid() + WIFSTOPPED and WIFCONTINUED.
  142. # Note: if a process is stopped it ignores SIGTERM.
  143. p.send_signal(signal.SIGSTOP)
  144. with pytest.raises(psutil.TimeoutExpired):
  145. p.wait(timeout=0.001)
  146. p.send_signal(signal.SIGCONT)
  147. with pytest.raises(psutil.TimeoutExpired):
  148. p.wait(timeout=0.001)
  149. p.send_signal(signal.SIGTERM)
  150. assert p.wait() == -signal.SIGTERM
  151. assert p.wait() == -signal.SIGTERM
  152. else:
  153. p.suspend()
  154. with pytest.raises(psutil.TimeoutExpired):
  155. p.wait(timeout=0.001)
  156. p.resume()
  157. with pytest.raises(psutil.TimeoutExpired):
  158. p.wait(timeout=0.001)
  159. p.terminate()
  160. assert p.wait() == signal.SIGTERM
  161. assert p.wait() == signal.SIGTERM
  162. def test_wait_non_children(self):
  163. # Test wait() against a process which is not our direct
  164. # child.
  165. child, grandchild = self.spawn_children_pair()
  166. with pytest.raises(psutil.TimeoutExpired):
  167. child.wait(0.01)
  168. with pytest.raises(psutil.TimeoutExpired):
  169. grandchild.wait(0.01)
  170. # We also terminate the direct child otherwise the
  171. # grandchild will hang until the parent is gone.
  172. child.terminate()
  173. grandchild.terminate()
  174. child_ret = child.wait()
  175. grandchild_ret = grandchild.wait()
  176. if POSIX:
  177. assert child_ret == -signal.SIGTERM
  178. # For processes which are not our children we're supposed
  179. # to get None.
  180. assert grandchild_ret is None
  181. else:
  182. assert child_ret == signal.SIGTERM
  183. assert child_ret == signal.SIGTERM
  184. def test_wait_timeout(self):
  185. p = self.spawn_psproc()
  186. p.name()
  187. with pytest.raises(psutil.TimeoutExpired):
  188. p.wait(0.01)
  189. with pytest.raises(psutil.TimeoutExpired):
  190. p.wait(0)
  191. with pytest.raises(ValueError):
  192. p.wait(-1)
  193. def test_wait_timeout_nonblocking(self):
  194. p = self.spawn_psproc()
  195. with pytest.raises(psutil.TimeoutExpired):
  196. p.wait(0)
  197. p.kill()
  198. stop_at = time.time() + GLOBAL_TIMEOUT
  199. while time.time() < stop_at:
  200. try:
  201. code = p.wait(0)
  202. break
  203. except psutil.TimeoutExpired:
  204. pass
  205. else:
  206. return pytest.fail('timeout')
  207. if POSIX:
  208. assert code == -signal.SIGKILL
  209. else:
  210. assert code == signal.SIGTERM
  211. self.assert_proc_gone(p)
  212. def test_cpu_percent(self):
  213. p = psutil.Process()
  214. p.cpu_percent(interval=0.001)
  215. p.cpu_percent(interval=0.001)
  216. for _ in range(100):
  217. percent = p.cpu_percent(interval=None)
  218. assert isinstance(percent, float)
  219. assert percent >= 0.0
  220. with pytest.raises(ValueError):
  221. p.cpu_percent(interval=-1)
  222. def test_cpu_percent_numcpus_none(self):
  223. # See: https://github.com/giampaolo/psutil/issues/1087
  224. with mock.patch('psutil.cpu_count', return_value=None) as m:
  225. psutil.Process().cpu_percent()
  226. assert m.called
  227. def test_cpu_times(self):
  228. times = psutil.Process().cpu_times()
  229. assert times.user >= 0.0, times
  230. assert times.system >= 0.0, times
  231. assert times.children_user >= 0.0, times
  232. assert times.children_system >= 0.0, times
  233. if LINUX:
  234. assert times.iowait >= 0.0, times
  235. # make sure returned values can be pretty printed with strftime
  236. for name in times._fields:
  237. time.strftime("%H:%M:%S", time.localtime(getattr(times, name)))
  238. @pytest.mark.skipif(not HAS_PROC_CPU_NUM, reason="not supported")
  239. def test_cpu_num(self):
  240. p = psutil.Process()
  241. num = p.cpu_num()
  242. assert num >= 0
  243. if psutil.cpu_count() == 1:
  244. assert num == 0
  245. assert p.cpu_num() in range(psutil.cpu_count())
  246. def test_create_time(self):
  247. p = self.spawn_psproc()
  248. now = time.time()
  249. # Fail if the difference with current time is > 2s.
  250. assert abs(p.create_time() - now) < 2
  251. # make sure returned value can be pretty printed with strftime
  252. time.strftime("%Y %m %d %H:%M:%S", time.localtime(p.create_time()))
  253. @pytest.mark.skipif(not POSIX, reason="POSIX only")
  254. def test_terminal(self):
  255. terminal = psutil.Process().terminal()
  256. if terminal is not None:
  257. try:
  258. tty = os.path.realpath(sh('tty'))
  259. except RuntimeError:
  260. # Note: happens if pytest is run without the `-s` opt.
  261. return pytest.skip("can't rely on `tty` CLI")
  262. else:
  263. assert terminal == tty
  264. @pytest.mark.skipif(not HAS_PROC_IO_COUNTERS, reason="not supported")
  265. @skip_on_not_implemented(only_if=LINUX)
  266. def test_io_counters(self):
  267. p = psutil.Process()
  268. # test reads
  269. io1 = p.io_counters()
  270. with open(PYTHON_EXE, 'rb') as f:
  271. f.read()
  272. io2 = p.io_counters()
  273. if not BSD and not AIX:
  274. assert io2.read_count > io1.read_count
  275. assert io2.write_count == io1.write_count
  276. if LINUX:
  277. assert io2.read_chars > io1.read_chars
  278. assert io2.write_chars == io1.write_chars
  279. else:
  280. assert io2.read_bytes >= io1.read_bytes
  281. assert io2.write_bytes >= io1.write_bytes
  282. # test writes
  283. io1 = p.io_counters()
  284. with open(self.get_testfn(), 'wb') as f:
  285. f.write(bytes("x" * 1000000, 'ascii'))
  286. io2 = p.io_counters()
  287. assert io2.write_count >= io1.write_count
  288. assert io2.write_bytes >= io1.write_bytes
  289. assert io2.read_count >= io1.read_count
  290. assert io2.read_bytes >= io1.read_bytes
  291. if LINUX:
  292. assert io2.write_chars > io1.write_chars
  293. assert io2.read_chars >= io1.read_chars
  294. # sanity check
  295. for i in range(len(io2)):
  296. if BSD and i >= 2:
  297. # On BSD read_bytes and write_bytes are always set to -1.
  298. continue
  299. assert io2[i] >= 0
  300. assert io2[i] >= 0
  301. @pytest.mark.skipif(not HAS_IONICE, reason="not supported")
  302. @pytest.mark.skipif(not LINUX, reason="linux only")
  303. def test_ionice_linux(self):
  304. def cleanup(init):
  305. ioclass, value = init
  306. if ioclass == psutil.IOPRIO_CLASS_NONE:
  307. value = 0
  308. p.ionice(ioclass, value)
  309. p = psutil.Process()
  310. if not CI_TESTING:
  311. assert p.ionice()[0] == psutil.IOPRIO_CLASS_NONE
  312. assert psutil.IOPRIO_CLASS_NONE == 0
  313. assert psutil.IOPRIO_CLASS_RT == 1 # high
  314. assert psutil.IOPRIO_CLASS_BE == 2 # normal
  315. assert psutil.IOPRIO_CLASS_IDLE == 3 # low
  316. init = p.ionice()
  317. self.addCleanup(cleanup, init)
  318. # low
  319. p.ionice(psutil.IOPRIO_CLASS_IDLE)
  320. assert tuple(p.ionice()) == (psutil.IOPRIO_CLASS_IDLE, 0)
  321. with pytest.raises(ValueError): # accepts no value
  322. p.ionice(psutil.IOPRIO_CLASS_IDLE, value=7)
  323. # normal
  324. p.ionice(psutil.IOPRIO_CLASS_BE)
  325. assert tuple(p.ionice()) == (psutil.IOPRIO_CLASS_BE, 0)
  326. p.ionice(psutil.IOPRIO_CLASS_BE, value=7)
  327. assert tuple(p.ionice()) == (psutil.IOPRIO_CLASS_BE, 7)
  328. with pytest.raises(ValueError):
  329. p.ionice(psutil.IOPRIO_CLASS_BE, value=8)
  330. try:
  331. p.ionice(psutil.IOPRIO_CLASS_RT, value=7)
  332. except psutil.AccessDenied:
  333. pass
  334. # errs
  335. with pytest.raises(ValueError, match="ioclass accepts no value"):
  336. p.ionice(psutil.IOPRIO_CLASS_NONE, 1)
  337. with pytest.raises(ValueError, match="ioclass accepts no value"):
  338. p.ionice(psutil.IOPRIO_CLASS_IDLE, 1)
  339. with pytest.raises(
  340. ValueError, match="'ioclass' argument must be specified"
  341. ):
  342. p.ionice(value=1)
  343. @pytest.mark.skipif(not HAS_IONICE, reason="not supported")
  344. @pytest.mark.skipif(
  345. not WINDOWS, reason="not supported on this win version"
  346. )
  347. def test_ionice_win(self):
  348. p = psutil.Process()
  349. if not CI_TESTING:
  350. assert p.ionice() == psutil.IOPRIO_NORMAL
  351. init = p.ionice()
  352. self.addCleanup(p.ionice, init)
  353. # base
  354. p.ionice(psutil.IOPRIO_VERYLOW)
  355. assert p.ionice() == psutil.IOPRIO_VERYLOW
  356. p.ionice(psutil.IOPRIO_LOW)
  357. assert p.ionice() == psutil.IOPRIO_LOW
  358. try:
  359. p.ionice(psutil.IOPRIO_HIGH)
  360. except psutil.AccessDenied:
  361. pass
  362. else:
  363. assert p.ionice() == psutil.IOPRIO_HIGH
  364. # errs
  365. with pytest.raises(
  366. TypeError, match="value argument not accepted on Windows"
  367. ):
  368. p.ionice(psutil.IOPRIO_NORMAL, value=1)
  369. with pytest.raises(ValueError, match="is not a valid priority"):
  370. p.ionice(psutil.IOPRIO_HIGH + 1)
  371. @pytest.mark.skipif(not HAS_RLIMIT, reason="not supported")
  372. def test_rlimit_get(self):
  373. import resource
  374. p = psutil.Process(os.getpid())
  375. names = [x for x in dir(psutil) if x.startswith('RLIMIT')]
  376. assert names, names
  377. for name in names:
  378. value = getattr(psutil, name)
  379. assert value >= 0
  380. if name in dir(resource):
  381. assert value == getattr(resource, name)
  382. # XXX - On PyPy RLIMIT_INFINITY returned by
  383. # resource.getrlimit() is reported as a very big long
  384. # number instead of -1. It looks like a bug with PyPy.
  385. if PYPY:
  386. continue
  387. assert p.rlimit(value) == resource.getrlimit(value)
  388. else:
  389. ret = p.rlimit(value)
  390. assert len(ret) == 2
  391. assert ret[0] >= -1
  392. assert ret[1] >= -1
  393. @pytest.mark.skipif(not HAS_RLIMIT, reason="not supported")
  394. def test_rlimit_set(self):
  395. p = self.spawn_psproc()
  396. p.rlimit(psutil.RLIMIT_NOFILE, (5, 5))
  397. assert p.rlimit(psutil.RLIMIT_NOFILE) == (5, 5)
  398. # If pid is 0 prlimit() applies to the calling process and
  399. # we don't want that.
  400. if LINUX:
  401. with pytest.raises(ValueError, match="can't use prlimit"):
  402. psutil._psplatform.Process(0).rlimit(0)
  403. with pytest.raises(ValueError):
  404. p.rlimit(psutil.RLIMIT_NOFILE, (5, 5, 5))
  405. @pytest.mark.skipif(not HAS_RLIMIT, reason="not supported")
  406. def test_rlimit(self):
  407. p = psutil.Process()
  408. testfn = self.get_testfn()
  409. soft, hard = p.rlimit(psutil.RLIMIT_FSIZE)
  410. try:
  411. p.rlimit(psutil.RLIMIT_FSIZE, (1024, hard))
  412. with open(testfn, "wb") as f:
  413. f.write(b"X" * 1024)
  414. # write() or flush() doesn't always cause the exception
  415. # but close() will.
  416. with pytest.raises(OSError) as exc:
  417. with open(testfn, "wb") as f:
  418. f.write(b"X" * 1025)
  419. assert exc.value.errno == errno.EFBIG
  420. finally:
  421. p.rlimit(psutil.RLIMIT_FSIZE, (soft, hard))
  422. assert p.rlimit(psutil.RLIMIT_FSIZE) == (soft, hard)
  423. @pytest.mark.skipif(not HAS_RLIMIT, reason="not supported")
  424. def test_rlimit_infinity(self):
  425. # First set a limit, then re-set it by specifying INFINITY
  426. # and assume we overridden the previous limit.
  427. p = psutil.Process()
  428. soft, hard = p.rlimit(psutil.RLIMIT_FSIZE)
  429. try:
  430. p.rlimit(psutil.RLIMIT_FSIZE, (1024, hard))
  431. p.rlimit(psutil.RLIMIT_FSIZE, (psutil.RLIM_INFINITY, hard))
  432. with open(self.get_testfn(), "wb") as f:
  433. f.write(b"X" * 2048)
  434. finally:
  435. p.rlimit(psutil.RLIMIT_FSIZE, (soft, hard))
  436. assert p.rlimit(psutil.RLIMIT_FSIZE) == (soft, hard)
  437. @pytest.mark.skipif(not HAS_RLIMIT, reason="not supported")
  438. def test_rlimit_infinity_value(self):
  439. # RLIMIT_FSIZE should be RLIM_INFINITY, which will be a really
  440. # big number on a platform with large file support. On these
  441. # platforms we need to test that the get/setrlimit functions
  442. # properly convert the number to a C long long and that the
  443. # conversion doesn't raise an error.
  444. p = psutil.Process()
  445. soft, hard = p.rlimit(psutil.RLIMIT_FSIZE)
  446. assert hard == psutil.RLIM_INFINITY
  447. p.rlimit(psutil.RLIMIT_FSIZE, (soft, hard))
  448. @pytest.mark.xdist_group(name="serial")
  449. def test_num_threads(self):
  450. # on certain platforms such as Linux we might test for exact
  451. # thread number, since we always have with 1 thread per process,
  452. # but this does not apply across all platforms (MACOS, Windows)
  453. p = psutil.Process()
  454. if OPENBSD:
  455. try:
  456. step1 = p.num_threads()
  457. except psutil.AccessDenied:
  458. return pytest.skip("on OpenBSD this requires root access")
  459. else:
  460. step1 = p.num_threads()
  461. with ThreadTask():
  462. step2 = p.num_threads()
  463. assert step2 == step1 + 1
  464. @pytest.mark.skipif(not WINDOWS, reason="WINDOWS only")
  465. def test_num_handles(self):
  466. # a better test is done later into test/_windows.py
  467. p = psutil.Process()
  468. assert p.num_handles() > 0
  469. @pytest.mark.skipif(not HAS_THREADS, reason="not supported")
  470. def test_threads(self):
  471. p = psutil.Process()
  472. if OPENBSD:
  473. try:
  474. step1 = p.threads()
  475. except psutil.AccessDenied:
  476. return pytest.skip("on OpenBSD this requires root access")
  477. else:
  478. step1 = p.threads()
  479. with ThreadTask():
  480. step2 = p.threads()
  481. assert len(step2) == len(step1) + 1
  482. athread = step2[0]
  483. # test named tuple
  484. assert athread.id == athread[0]
  485. assert athread.user_time == athread[1]
  486. assert athread.system_time == athread[2]
  487. @retry_on_failure()
  488. @skip_on_access_denied(only_if=MACOS)
  489. @pytest.mark.skipif(not HAS_THREADS, reason="not supported")
  490. def test_threads_2(self):
  491. p = self.spawn_psproc()
  492. if OPENBSD:
  493. try:
  494. p.threads()
  495. except psutil.AccessDenied:
  496. return pytest.skip("on OpenBSD this requires root access")
  497. assert (
  498. abs(p.cpu_times().user - sum(x.user_time for x in p.threads()))
  499. < 0.1
  500. )
  501. assert (
  502. abs(p.cpu_times().system - sum(x.system_time for x in p.threads()))
  503. < 0.1
  504. )
  505. @retry_on_failure()
  506. def test_memory_info(self):
  507. p = psutil.Process()
  508. # step 1 - get a base value to compare our results
  509. rss1, vms1 = p.memory_info()[:2]
  510. percent1 = p.memory_percent()
  511. assert rss1 > 0
  512. assert vms1 > 0
  513. # step 2 - allocate some memory
  514. memarr = [None] * 1500000
  515. rss2, vms2 = p.memory_info()[:2]
  516. percent2 = p.memory_percent()
  517. # step 3 - make sure that the memory usage bumped up
  518. assert rss2 > rss1
  519. assert vms2 >= vms1 # vms might be equal
  520. assert percent2 > percent1
  521. del memarr
  522. if WINDOWS:
  523. mem = p.memory_info()
  524. assert mem.rss == mem.wset
  525. assert mem.vms == mem.pagefile
  526. mem = p.memory_info()
  527. for name in mem._fields:
  528. assert getattr(mem, name) >= 0
  529. def test_memory_full_info(self):
  530. p = psutil.Process()
  531. total = psutil.virtual_memory().total
  532. mem = p.memory_full_info()
  533. for name in mem._fields:
  534. value = getattr(mem, name)
  535. assert value >= 0
  536. if (name == "vms" and OSX) or LINUX:
  537. continue
  538. assert value <= total
  539. if LINUX or WINDOWS or MACOS:
  540. assert mem.uss >= 0
  541. if LINUX:
  542. assert mem.pss >= 0
  543. assert mem.swap >= 0
  544. @pytest.mark.skipif(not HAS_MEMORY_MAPS, reason="not supported")
  545. def test_memory_maps(self):
  546. p = psutil.Process()
  547. maps = p.memory_maps()
  548. assert len(maps) == len(set(maps))
  549. ext_maps = p.memory_maps(grouped=False)
  550. for nt in maps:
  551. if nt.path.startswith('['):
  552. continue
  553. if BSD and nt.path == "pvclock":
  554. continue
  555. assert os.path.isabs(nt.path), nt.path
  556. if POSIX:
  557. try:
  558. assert os.path.exists(nt.path) or os.path.islink(
  559. nt.path
  560. ), nt.path
  561. except AssertionError:
  562. if not LINUX:
  563. raise
  564. # https://github.com/giampaolo/psutil/issues/759
  565. with open_text('/proc/self/smaps') as f:
  566. data = f.read()
  567. if f"{nt.path} (deleted)" not in data:
  568. raise
  569. elif '64' not in os.path.basename(nt.path):
  570. # XXX - On Windows we have this strange behavior with
  571. # 64 bit dlls: they are visible via explorer but cannot
  572. # be accessed via os.stat() (wtf?).
  573. try:
  574. st = os.stat(nt.path)
  575. except FileNotFoundError:
  576. pass
  577. else:
  578. assert stat.S_ISREG(st.st_mode), nt.path
  579. for nt in ext_maps:
  580. for fname in nt._fields:
  581. value = getattr(nt, fname)
  582. if fname == 'path':
  583. continue
  584. if fname in {'addr', 'perms'}:
  585. assert value, value
  586. else:
  587. assert isinstance(value, int)
  588. assert value >= 0, value
  589. @pytest.mark.skipif(not HAS_MEMORY_MAPS, reason="not supported")
  590. def test_memory_maps_lists_lib(self):
  591. # Make sure a newly loaded shared lib is listed.
  592. p = psutil.Process()
  593. with copyload_shared_lib() as path:
  594. def normpath(p):
  595. return os.path.realpath(os.path.normcase(p))
  596. libpaths = [normpath(x.path) for x in p.memory_maps()]
  597. assert normpath(path) in libpaths
  598. def test_memory_percent(self):
  599. p = psutil.Process()
  600. p.memory_percent()
  601. with pytest.raises(ValueError):
  602. p.memory_percent(memtype="?!?")
  603. if LINUX or MACOS or WINDOWS:
  604. p.memory_percent(memtype='uss')
  605. def test_is_running(self):
  606. p = self.spawn_psproc()
  607. assert p.is_running()
  608. assert p.is_running()
  609. p.kill()
  610. p.wait()
  611. assert not p.is_running()
  612. assert not p.is_running()
  613. def test_exe(self):
  614. p = self.spawn_psproc()
  615. exe = p.exe()
  616. try:
  617. assert exe == PYTHON_EXE
  618. except AssertionError:
  619. if WINDOWS and len(exe) == len(PYTHON_EXE):
  620. # on Windows we don't care about case sensitivity
  621. normcase = os.path.normcase
  622. assert normcase(exe) == normcase(PYTHON_EXE)
  623. else:
  624. # certain platforms such as BSD are more accurate returning:
  625. # "/usr/local/bin/python3.7"
  626. # ...instead of:
  627. # "/usr/local/bin/python"
  628. # We do not want to consider this difference in accuracy
  629. # an error.
  630. ver = f"{sys.version_info[0]}.{sys.version_info[1]}"
  631. try:
  632. assert exe.replace(ver, '') == PYTHON_EXE.replace(ver, '')
  633. except AssertionError:
  634. # Typically MACOS. Really not sure what to do here.
  635. pass
  636. out = sh([exe, "-c", "import os; print('hey')"])
  637. assert out == 'hey'
  638. def test_cmdline(self):
  639. cmdline = [
  640. PYTHON_EXE,
  641. "-c",
  642. "import time; [time.sleep(0.1) for x in range(100)]",
  643. ]
  644. p = self.spawn_psproc(cmdline)
  645. if NETBSD and p.cmdline() == []:
  646. # https://github.com/giampaolo/psutil/issues/2250
  647. return pytest.skip("OPENBSD: returned EBUSY")
  648. # XXX - most of the times the underlying sysctl() call on Net
  649. # and Open BSD returns a truncated string.
  650. # Also /proc/pid/cmdline behaves the same so it looks
  651. # like this is a kernel bug.
  652. # XXX - AIX truncates long arguments in /proc/pid/cmdline
  653. if NETBSD or OPENBSD or AIX:
  654. assert p.cmdline()[0] == PYTHON_EXE
  655. else:
  656. if MACOS and CI_TESTING:
  657. pyexe = p.cmdline()[0]
  658. if pyexe != PYTHON_EXE:
  659. assert ' '.join(p.cmdline()[1:]) == ' '.join(cmdline[1:])
  660. return None
  661. assert ' '.join(p.cmdline()) == ' '.join(cmdline)
  662. def test_long_cmdline(self):
  663. cmdline = [PYTHON_EXE]
  664. cmdline.extend(["-v"] * 50)
  665. cmdline.extend(
  666. ["-c", "import time; [time.sleep(0.1) for x in range(100)]"]
  667. )
  668. p = self.spawn_psproc(cmdline)
  669. # XXX - flaky test: exclude the python exe which, for some
  670. # reason, and only sometimes, on OSX appears different.
  671. cmdline = cmdline[1:]
  672. if OPENBSD:
  673. # XXX: for some reason the test process may turn into a
  674. # zombie (don't know why).
  675. try:
  676. assert p.cmdline()[1:] == cmdline
  677. except psutil.ZombieProcess:
  678. return pytest.skip("OPENBSD: process turned into zombie")
  679. else:
  680. ret = p.cmdline()[1:]
  681. if NETBSD and ret == []:
  682. # https://github.com/giampaolo/psutil/issues/2250
  683. return pytest.skip("OPENBSD: returned EBUSY")
  684. assert ret == cmdline
  685. def test_name(self):
  686. p = self.spawn_psproc()
  687. name = p.name().lower()
  688. if name.endswith("t"): # in the free-threaded build
  689. name = name[:-1]
  690. pyexe = os.path.basename(os.path.realpath(sys.executable)).lower()
  691. assert pyexe.startswith(name), (pyexe, name)
  692. @retry_on_failure()
  693. def test_long_name(self):
  694. pyexe = create_py_exe(self.get_testfn(suffix=string.digits * 2))
  695. cmdline = [
  696. pyexe,
  697. "-c",
  698. "import time; [time.sleep(0.1) for x in range(100)]",
  699. ]
  700. p = self.spawn_psproc(cmdline)
  701. if OPENBSD:
  702. # XXX: for some reason the test process may turn into a
  703. # zombie (don't know why). Because the name() is long, all
  704. # UNIX kernels truncate it to 15 chars, so internally psutil
  705. # tries to guess the full name() from the cmdline(). But the
  706. # cmdline() of a zombie on OpenBSD fails (internally), so we
  707. # just compare the first 15 chars. Full explanation:
  708. # https://github.com/giampaolo/psutil/issues/2239
  709. try:
  710. assert p.name() == os.path.basename(pyexe)
  711. except AssertionError:
  712. if p.status() == psutil.STATUS_ZOMBIE:
  713. assert os.path.basename(pyexe).startswith(p.name())
  714. else:
  715. raise
  716. else:
  717. assert p.name() == os.path.basename(pyexe)
  718. @pytest.mark.skipif(not POSIX, reason="POSIX only")
  719. def test_uids(self):
  720. p = psutil.Process()
  721. real, effective, _saved = p.uids()
  722. # os.getuid() refers to "real" uid
  723. assert real == os.getuid()
  724. # os.geteuid() refers to "effective" uid
  725. assert effective == os.geteuid()
  726. # No such thing as os.getsuid() ("saved" uid), but we have
  727. # os.getresuid() which returns all of them.
  728. if hasattr(os, "getresuid"):
  729. assert os.getresuid() == p.uids()
  730. @pytest.mark.skipif(not POSIX, reason="POSIX only")
  731. def test_gids(self):
  732. p = psutil.Process()
  733. real, effective, _saved = p.gids()
  734. # os.getuid() refers to "real" uid
  735. assert real == os.getgid()
  736. # os.geteuid() refers to "effective" uid
  737. assert effective == os.getegid()
  738. # No such thing as os.getsgid() ("saved" gid), but we have
  739. # os.getresgid() which returns all of them.
  740. if hasattr(os, "getresuid"):
  741. assert os.getresgid() == p.gids()
  742. def test_nice(self):
  743. def cleanup(init):
  744. try:
  745. p.nice(init)
  746. except psutil.AccessDenied:
  747. pass
  748. p = psutil.Process()
  749. with pytest.raises(TypeError):
  750. p.nice("str")
  751. init = p.nice()
  752. self.addCleanup(cleanup, init)
  753. if WINDOWS:
  754. highest_prio = None
  755. for prio in [
  756. psutil.IDLE_PRIORITY_CLASS,
  757. psutil.BELOW_NORMAL_PRIORITY_CLASS,
  758. psutil.NORMAL_PRIORITY_CLASS,
  759. psutil.ABOVE_NORMAL_PRIORITY_CLASS,
  760. psutil.HIGH_PRIORITY_CLASS,
  761. psutil.REALTIME_PRIORITY_CLASS,
  762. ]:
  763. with self.subTest(prio=prio):
  764. try:
  765. p.nice(prio)
  766. except psutil.AccessDenied:
  767. pass
  768. else:
  769. new_prio = p.nice()
  770. # The OS may limit our maximum priority,
  771. # even if the function succeeds. For higher
  772. # priorities, we match either the expected
  773. # value or the highest so far.
  774. if prio in {
  775. psutil.ABOVE_NORMAL_PRIORITY_CLASS,
  776. psutil.HIGH_PRIORITY_CLASS,
  777. psutil.REALTIME_PRIORITY_CLASS,
  778. }:
  779. if new_prio == prio or highest_prio is None:
  780. highest_prio = prio
  781. assert new_prio == highest_prio
  782. else:
  783. assert new_prio == prio
  784. else:
  785. try:
  786. if hasattr(os, "getpriority"):
  787. assert (
  788. os.getpriority(os.PRIO_PROCESS, os.getpid())
  789. == p.nice()
  790. )
  791. p.nice(1)
  792. assert p.nice() == 1
  793. if hasattr(os, "getpriority"):
  794. assert (
  795. os.getpriority(os.PRIO_PROCESS, os.getpid())
  796. == p.nice()
  797. )
  798. # XXX - going back to previous nice value raises
  799. # AccessDenied on MACOS
  800. if not MACOS:
  801. p.nice(0)
  802. assert p.nice() == 0
  803. except psutil.AccessDenied:
  804. pass
  805. def test_status(self):
  806. p = psutil.Process()
  807. assert p.status() == psutil.STATUS_RUNNING
  808. def test_username(self):
  809. p = self.spawn_psproc()
  810. username = p.username()
  811. if WINDOWS:
  812. domain, username = username.split('\\')
  813. getpass_user = getpass.getuser()
  814. if getpass_user.endswith('$'):
  815. # When running as a service account (most likely to be
  816. # NetworkService), these user name calculations don't produce
  817. # the same result, causing the test to fail.
  818. return pytest.skip('running as service account')
  819. assert username == getpass_user
  820. if 'USERDOMAIN' in os.environ:
  821. assert domain == os.environ['USERDOMAIN']
  822. else:
  823. assert username == getpass.getuser()
  824. def test_cwd(self):
  825. p = self.spawn_psproc()
  826. assert p.cwd() == os.getcwd()
  827. def test_cwd_2(self):
  828. cmd = [
  829. PYTHON_EXE,
  830. "-c",
  831. (
  832. "import os, time; os.chdir('..'); [time.sleep(0.1) for x in"
  833. " range(100)]"
  834. ),
  835. ]
  836. p = self.spawn_psproc(cmd)
  837. call_until(lambda: p.cwd() == os.path.dirname(os.getcwd()))
  838. @pytest.mark.skipif(not HAS_CPU_AFFINITY, reason="not supported")
  839. def test_cpu_affinity(self):
  840. p = psutil.Process()
  841. initial = p.cpu_affinity()
  842. assert initial, initial
  843. self.addCleanup(p.cpu_affinity, initial)
  844. if hasattr(os, "sched_getaffinity"):
  845. assert initial == list(os.sched_getaffinity(p.pid))
  846. assert len(initial) == len(set(initial))
  847. all_cpus = list(range(len(psutil.cpu_percent(percpu=True))))
  848. for n in all_cpus:
  849. p.cpu_affinity([n])
  850. assert p.cpu_affinity() == [n]
  851. if hasattr(os, "sched_getaffinity"):
  852. assert p.cpu_affinity() == list(os.sched_getaffinity(p.pid))
  853. # also test num_cpu()
  854. if hasattr(p, "num_cpu"):
  855. assert p.cpu_affinity()[0] == p.num_cpu()
  856. # [] is an alias for "all eligible CPUs"; on Linux this may
  857. # not be equal to all available CPUs, see:
  858. # https://github.com/giampaolo/psutil/issues/956
  859. p.cpu_affinity([])
  860. if LINUX:
  861. assert p.cpu_affinity() == p._proc._get_eligible_cpus()
  862. else:
  863. assert p.cpu_affinity() == all_cpus
  864. if hasattr(os, "sched_getaffinity"):
  865. assert p.cpu_affinity() == list(os.sched_getaffinity(p.pid))
  866. with pytest.raises(TypeError):
  867. p.cpu_affinity(1)
  868. p.cpu_affinity(initial)
  869. # it should work with all iterables, not only lists
  870. p.cpu_affinity(set(all_cpus))
  871. p.cpu_affinity(tuple(all_cpus))
  872. @pytest.mark.skipif(not HAS_CPU_AFFINITY, reason="not supported")
  873. def test_cpu_affinity_errs(self):
  874. p = self.spawn_psproc()
  875. invalid_cpu = [len(psutil.cpu_times(percpu=True)) + 10]
  876. with pytest.raises(ValueError):
  877. p.cpu_affinity(invalid_cpu)
  878. with pytest.raises(ValueError):
  879. p.cpu_affinity(range(10000, 11000))
  880. with pytest.raises((TypeError, ValueError)):
  881. p.cpu_affinity([0, "1"])
  882. with pytest.raises(ValueError):
  883. p.cpu_affinity([0, -1])
  884. @pytest.mark.skipif(not HAS_CPU_AFFINITY, reason="not supported")
  885. def test_cpu_affinity_all_combinations(self):
  886. p = psutil.Process()
  887. initial = p.cpu_affinity()
  888. assert initial, initial
  889. self.addCleanup(p.cpu_affinity, initial)
  890. # All possible CPU set combinations.
  891. if len(initial) > 12:
  892. initial = initial[:12] # ...otherwise it will take forever
  893. combos = []
  894. for i in range(len(initial) + 1):
  895. combos.extend(
  896. list(subset)
  897. for subset in itertools.combinations(initial, i)
  898. if subset
  899. )
  900. for combo in combos:
  901. p.cpu_affinity(combo)
  902. assert sorted(p.cpu_affinity()) == sorted(combo)
  903. # TODO: #595
  904. @pytest.mark.skipif(BSD, reason="broken on BSD")
  905. def test_open_files(self):
  906. p = psutil.Process()
  907. testfn = self.get_testfn()
  908. files = p.open_files()
  909. assert testfn not in files
  910. with open(testfn, 'wb') as f:
  911. f.write(b'x' * 1024)
  912. f.flush()
  913. # give the kernel some time to see the new file
  914. call_until(lambda: len(p.open_files()) != len(files))
  915. files = p.open_files()
  916. filenames = [os.path.normcase(x.path) for x in files]
  917. assert os.path.normcase(testfn) in filenames
  918. if LINUX:
  919. for file in files:
  920. if file.path == testfn:
  921. assert file.position == 1024
  922. for file in files:
  923. assert os.path.isfile(file.path), file
  924. # another process
  925. cmdline = (
  926. f"import time; f = open(r'{testfn}', 'r'); [time.sleep(0.1) for x"
  927. " in range(100)];"
  928. )
  929. p = self.spawn_psproc([PYTHON_EXE, "-c", cmdline])
  930. for x in range(100):
  931. filenames = [os.path.normcase(x.path) for x in p.open_files()]
  932. if testfn in filenames:
  933. break
  934. time.sleep(0.01)
  935. else:
  936. assert os.path.normcase(testfn) in filenames
  937. for file in filenames:
  938. assert os.path.isfile(file), file
  939. # TODO: #595
  940. @pytest.mark.skipif(BSD, reason="broken on BSD")
  941. def test_open_files_2(self):
  942. # test fd and path fields
  943. p = psutil.Process()
  944. normcase = os.path.normcase
  945. testfn = self.get_testfn()
  946. with open(testfn, 'w') as fileobj:
  947. for file in p.open_files():
  948. if (
  949. normcase(file.path) == normcase(fileobj.name)
  950. or file.fd == fileobj.fileno()
  951. ):
  952. break
  953. else:
  954. return pytest.fail(f"no file found; files={p.open_files()!r}")
  955. assert normcase(file.path) == normcase(fileobj.name)
  956. if WINDOWS:
  957. assert file.fd == -1
  958. else:
  959. assert file.fd == fileobj.fileno()
  960. # test positions
  961. ntuple = p.open_files()[0]
  962. assert ntuple[0] == ntuple.path
  963. assert ntuple[1] == ntuple.fd
  964. # test file is gone
  965. assert fileobj.name not in p.open_files()
  966. @pytest.mark.skipif(not POSIX, reason="POSIX only")
  967. @pytest.mark.xdist_group(name="serial")
  968. def test_num_fds(self):
  969. p = psutil.Process()
  970. testfn = self.get_testfn()
  971. start = p.num_fds()
  972. with open(testfn, 'w'):
  973. assert p.num_fds() == start + 1
  974. with socket.socket():
  975. assert p.num_fds() == start + 2
  976. assert p.num_fds() == start
  977. @skip_on_not_implemented(only_if=LINUX)
  978. @pytest.mark.skipif(
  979. OPENBSD or NETBSD, reason="not reliable on OPENBSD & NETBSD"
  980. )
  981. def test_num_ctx_switches(self):
  982. p = psutil.Process()
  983. before = sum(p.num_ctx_switches())
  984. for _ in range(2):
  985. time.sleep(0.05) # this shall ensure a context switch happens
  986. after = sum(p.num_ctx_switches())
  987. if after > before:
  988. return None
  989. return pytest.fail(
  990. "num ctx switches still the same after 2 iterations"
  991. )
  992. def test_ppid(self):
  993. p = psutil.Process()
  994. if hasattr(os, 'getppid'):
  995. assert p.ppid() == os.getppid()
  996. p = self.spawn_psproc()
  997. assert p.ppid() == os.getpid()
  998. def test_parent(self):
  999. p = self.spawn_psproc()
  1000. assert p.parent().pid == os.getpid()
  1001. lowest_pid = psutil.pids()[0]
  1002. assert psutil.Process(lowest_pid).parent() is None
  1003. def test_parent_mocked_ctime(self):
  1004. # Make sure we get a fresh copy of the ctime before processing
  1005. # parent().We make the assumption that the parent pid MUST have
  1006. # a creation time < than the child. If system clock is updated
  1007. # this assumption was broken.
  1008. # https://github.com/giampaolo/psutil/issues/2542
  1009. p = self.spawn_psproc()
  1010. p.create_time() # trigger cache
  1011. assert p._create_time
  1012. p._create_time = 1
  1013. assert p.parent().pid == os.getpid()
  1014. def test_parent_multi(self):
  1015. parent = psutil.Process()
  1016. child, grandchild = self.spawn_children_pair()
  1017. assert grandchild.parent() == child
  1018. assert child.parent() == parent
  1019. @retry_on_failure()
  1020. def test_parents(self):
  1021. parent = psutil.Process()
  1022. assert parent.parents()
  1023. child, grandchild = self.spawn_children_pair()
  1024. assert child.parents()[0] == parent
  1025. assert grandchild.parents()[0] == child
  1026. assert grandchild.parents()[1] == parent
  1027. def test_children(self):
  1028. parent = psutil.Process()
  1029. assert not parent.children()
  1030. assert not parent.children(recursive=True)
  1031. # On Windows we set the flag to 0 in order to cancel out the
  1032. # CREATE_NO_WINDOW flag (enabled by default) which creates
  1033. # an extra "conhost.exe" child.
  1034. child = self.spawn_psproc(creationflags=0)
  1035. children1 = parent.children()
  1036. children2 = parent.children(recursive=True)
  1037. for children in (children1, children2):
  1038. assert len(children) == 1
  1039. assert children[0].pid == child.pid
  1040. assert children[0].ppid() == parent.pid
  1041. def test_children_mocked_ctime(self):
  1042. # Make sure we get a fresh copy of the ctime before processing
  1043. # children(). We make the assumption that process children MUST
  1044. # have a creation time > than the parent. If system clock is
  1045. # updated this assumption was broken.
  1046. # https://github.com/giampaolo/psutil/issues/2542
  1047. parent = psutil.Process()
  1048. parent.create_time() # trigger cache
  1049. assert parent._create_time
  1050. parent._create_time += 100000
  1051. assert not parent.children()
  1052. assert not parent.children(recursive=True)
  1053. # On Windows we set the flag to 0 in order to cancel out the
  1054. # CREATE_NO_WINDOW flag (enabled by default) which creates
  1055. # an extra "conhost.exe" child.
  1056. child = self.spawn_psproc(creationflags=0)
  1057. children1 = parent.children()
  1058. children2 = parent.children(recursive=True)
  1059. for children in (children1, children2):
  1060. assert len(children) == 1
  1061. assert children[0].pid == child.pid
  1062. assert children[0].ppid() == parent.pid
  1063. def test_children_recursive(self):
  1064. # Test children() against two sub processes, p1 and p2, where
  1065. # p1 (our child) spawned p2 (our grandchild).
  1066. parent = psutil.Process()
  1067. child, grandchild = self.spawn_children_pair()
  1068. assert parent.children() == [child]
  1069. assert parent.children(recursive=True) == [child, grandchild]
  1070. # If the intermediate process is gone there's no way for
  1071. # children() to recursively find it.
  1072. child.terminate()
  1073. child.wait()
  1074. assert not parent.children(recursive=True)
  1075. def test_children_duplicates(self):
  1076. # find the process which has the highest number of children
  1077. table = collections.defaultdict(int)
  1078. for p in psutil.process_iter():
  1079. try:
  1080. table[p.ppid()] += 1
  1081. except psutil.Error:
  1082. pass
  1083. # this is the one, now let's make sure there are no duplicates
  1084. pid = max(table.items(), key=lambda x: x[1])[0]
  1085. if LINUX and pid == 0:
  1086. return pytest.skip("PID 0")
  1087. p = psutil.Process(pid)
  1088. try:
  1089. c = p.children(recursive=True)
  1090. except psutil.AccessDenied: # windows
  1091. pass
  1092. else:
  1093. assert len(c) == len(set(c))
  1094. def test_parents_and_children(self):
  1095. parent = psutil.Process()
  1096. child, grandchild = self.spawn_children_pair()
  1097. # forward
  1098. children = parent.children(recursive=True)
  1099. assert len(children) == 2
  1100. assert children[0] == child
  1101. assert children[1] == grandchild
  1102. # backward
  1103. parents = grandchild.parents()
  1104. assert parents[0] == child
  1105. assert parents[1] == parent
  1106. def test_suspend_resume(self):
  1107. p = self.spawn_psproc()
  1108. p.suspend()
  1109. for _ in range(100):
  1110. if p.status() == psutil.STATUS_STOPPED:
  1111. break
  1112. time.sleep(0.01)
  1113. p.resume()
  1114. assert p.status() != psutil.STATUS_STOPPED
  1115. def test_invalid_pid(self):
  1116. with pytest.raises(TypeError):
  1117. psutil.Process("1")
  1118. with pytest.raises(ValueError):
  1119. psutil.Process(-1)
  1120. def test_as_dict(self):
  1121. p = psutil.Process()
  1122. d = p.as_dict(attrs=['exe', 'name'])
  1123. assert sorted(d.keys()) == ['exe', 'name']
  1124. p = psutil.Process(min(psutil.pids()))
  1125. d = p.as_dict(attrs=['net_connections'], ad_value='foo')
  1126. if not isinstance(d['net_connections'], list):
  1127. assert d['net_connections'] == 'foo'
  1128. # Test ad_value is set on AccessDenied.
  1129. with mock.patch(
  1130. 'psutil.Process.nice', create=True, side_effect=psutil.AccessDenied
  1131. ):
  1132. assert p.as_dict(attrs=["nice"], ad_value=1) == {"nice": 1}
  1133. # Test that NoSuchProcess bubbles up.
  1134. with mock.patch(
  1135. 'psutil.Process.nice',
  1136. create=True,
  1137. side_effect=psutil.NoSuchProcess(p.pid, "name"),
  1138. ):
  1139. with pytest.raises(psutil.NoSuchProcess):
  1140. p.as_dict(attrs=["nice"])
  1141. # Test that ZombieProcess is swallowed.
  1142. with mock.patch(
  1143. 'psutil.Process.nice',
  1144. create=True,
  1145. side_effect=psutil.ZombieProcess(p.pid, "name"),
  1146. ):
  1147. assert p.as_dict(attrs=["nice"], ad_value="foo") == {"nice": "foo"}
  1148. # By default APIs raising NotImplementedError are
  1149. # supposed to be skipped.
  1150. with mock.patch(
  1151. 'psutil.Process.nice', create=True, side_effect=NotImplementedError
  1152. ):
  1153. d = p.as_dict()
  1154. assert 'nice' not in list(d.keys())
  1155. # ...unless the user explicitly asked for some attr.
  1156. with pytest.raises(NotImplementedError):
  1157. p.as_dict(attrs=["nice"])
  1158. # errors
  1159. with pytest.raises(TypeError):
  1160. p.as_dict('name')
  1161. with pytest.raises(ValueError):
  1162. p.as_dict(['foo'])
  1163. with pytest.raises(ValueError):
  1164. p.as_dict(['foo', 'bar'])
  1165. def test_oneshot(self):
  1166. p = psutil.Process()
  1167. with mock.patch("psutil._psplatform.Process.cpu_times") as m:
  1168. with p.oneshot():
  1169. p.cpu_times()
  1170. p.cpu_times()
  1171. assert m.call_count == 1
  1172. with mock.patch("psutil._psplatform.Process.cpu_times") as m:
  1173. p.cpu_times()
  1174. p.cpu_times()
  1175. assert m.call_count == 2
  1176. def test_oneshot_twice(self):
  1177. # Test the case where the ctx manager is __enter__ed twice.
  1178. # The second __enter__ is supposed to resut in a NOOP.
  1179. p = psutil.Process()
  1180. with mock.patch("psutil._psplatform.Process.cpu_times") as m1:
  1181. with mock.patch("psutil._psplatform.Process.oneshot_enter") as m2:
  1182. with p.oneshot():
  1183. p.cpu_times()
  1184. p.cpu_times()
  1185. with p.oneshot():
  1186. p.cpu_times()
  1187. p.cpu_times()
  1188. assert m1.call_count == 1
  1189. assert m2.call_count == 1
  1190. with mock.patch("psutil._psplatform.Process.cpu_times") as m:
  1191. p.cpu_times()
  1192. p.cpu_times()
  1193. assert m.call_count == 2
  1194. def test_oneshot_cache(self):
  1195. # Make sure oneshot() cache is nonglobal. Instead it's
  1196. # supposed to be bound to the Process instance, see:
  1197. # https://github.com/giampaolo/psutil/issues/1373
  1198. p1, p2 = self.spawn_children_pair()
  1199. p1_ppid = p1.ppid()
  1200. p2_ppid = p2.ppid()
  1201. assert p1_ppid != p2_ppid
  1202. with p1.oneshot():
  1203. assert p1.ppid() == p1_ppid
  1204. assert p2.ppid() == p2_ppid
  1205. with p2.oneshot():
  1206. assert p1.ppid() == p1_ppid
  1207. assert p2.ppid() == p2_ppid
  1208. def test_halfway_terminated_process(self):
  1209. # Test that NoSuchProcess exception gets raised in case the
  1210. # process dies after we create the Process object.
  1211. # Example:
  1212. # >>> proc = Process(1234)
  1213. # >>> time.sleep(2) # time-consuming task, process dies in meantime
  1214. # >>> proc.name()
  1215. # Refers to Issue #15
  1216. def assert_raises_nsp(fun, fun_name):
  1217. try:
  1218. ret = fun()
  1219. except psutil.ZombieProcess: # differentiate from NSP
  1220. raise
  1221. except psutil.NoSuchProcess:
  1222. pass
  1223. except psutil.AccessDenied:
  1224. if OPENBSD and fun_name in {'threads', 'num_threads'}:
  1225. return None
  1226. raise
  1227. else:
  1228. # NtQuerySystemInformation succeeds even if process is gone.
  1229. if WINDOWS and fun_name in {'exe', 'name'}:
  1230. return None
  1231. return pytest.fail(
  1232. f"{fun!r} didn't raise NSP and returned {ret!r} instead"
  1233. )
  1234. p = self.spawn_psproc()
  1235. p.terminate()
  1236. p.wait()
  1237. if WINDOWS: # XXX
  1238. call_until(lambda: p.pid not in psutil.pids())
  1239. self.assert_proc_gone(p)
  1240. ns = process_namespace(p)
  1241. for fun, name in ns.iter(ns.all):
  1242. assert_raises_nsp(fun, name)
  1243. @pytest.mark.skipif(not POSIX, reason="POSIX only")
  1244. def test_zombie_process(self):
  1245. _parent, zombie = self.spawn_zombie()
  1246. self.assert_proc_zombie(zombie)
  1247. if hasattr(psutil._psplatform.cext, "proc_is_zombie"):
  1248. assert not psutil._psplatform.cext.proc_is_zombie(os.getpid())
  1249. assert psutil._psplatform.cext.proc_is_zombie(zombie.pid)
  1250. @pytest.mark.skipif(not POSIX, reason="POSIX only")
  1251. def test_zombie_process_is_running_w_exc(self):
  1252. # Emulate a case where internally is_running() raises
  1253. # ZombieProcess.
  1254. p = psutil.Process()
  1255. with mock.patch(
  1256. "psutil.Process", side_effect=psutil.ZombieProcess(0)
  1257. ) as m:
  1258. assert p.is_running()
  1259. assert m.called
  1260. @pytest.mark.skipif(not POSIX, reason="POSIX only")
  1261. def test_zombie_process_status_w_exc(self):
  1262. # Emulate a case where internally status() raises
  1263. # ZombieProcess.
  1264. p = psutil.Process()
  1265. with mock.patch(
  1266. "psutil._psplatform.Process.status",
  1267. side_effect=psutil.ZombieProcess(0),
  1268. ) as m:
  1269. assert p.status() == psutil.STATUS_ZOMBIE
  1270. assert m.called
  1271. def test_reused_pid(self):
  1272. # Emulate a case where PID has been reused by another process.
  1273. subp = self.spawn_subproc()
  1274. p = psutil.Process(subp.pid)
  1275. p._ident = (p.pid, p.create_time() + 100)
  1276. list(psutil.process_iter())
  1277. assert p.pid in psutil._pmap
  1278. assert not p.is_running()
  1279. # make sure is_running() removed PID from process_iter()
  1280. # internal cache
  1281. with mock.patch.object(psutil._common, "PSUTIL_DEBUG", True):
  1282. with contextlib.redirect_stderr(io.StringIO()) as f:
  1283. list(psutil.process_iter())
  1284. assert (
  1285. f"refreshing Process instance for reused PID {p.pid}"
  1286. in f.getvalue()
  1287. )
  1288. assert p.pid not in psutil._pmap
  1289. assert p != psutil.Process(subp.pid)
  1290. msg = "process no longer exists and its PID has been reused"
  1291. ns = process_namespace(p)
  1292. for fun, name in ns.iter(ns.setters + ns.killers, clear_cache=False):
  1293. with self.subTest(name=name):
  1294. with pytest.raises(psutil.NoSuchProcess, match=msg):
  1295. fun()
  1296. assert "terminated + PID reused" in str(p)
  1297. assert "terminated + PID reused" in repr(p)
  1298. with pytest.raises(psutil.NoSuchProcess, match=msg):
  1299. p.ppid()
  1300. with pytest.raises(psutil.NoSuchProcess, match=msg):
  1301. p.parent()
  1302. with pytest.raises(psutil.NoSuchProcess, match=msg):
  1303. p.parents()
  1304. with pytest.raises(psutil.NoSuchProcess, match=msg):
  1305. p.children()
  1306. def test_pid_0(self):
  1307. # Process(0) is supposed to work on all platforms except Linux
  1308. if 0 not in psutil.pids():
  1309. with pytest.raises(psutil.NoSuchProcess):
  1310. psutil.Process(0)
  1311. # These 2 are a contradiction, but "ps" says PID 1's parent
  1312. # is PID 0.
  1313. assert not psutil.pid_exists(0)
  1314. assert psutil.Process(1).ppid() == 0
  1315. return
  1316. p = psutil.Process(0)
  1317. exc = psutil.AccessDenied if WINDOWS else ValueError
  1318. with pytest.raises(exc):
  1319. p.wait()
  1320. with pytest.raises(exc):
  1321. p.terminate()
  1322. with pytest.raises(exc):
  1323. p.suspend()
  1324. with pytest.raises(exc):
  1325. p.resume()
  1326. with pytest.raises(exc):
  1327. p.kill()
  1328. with pytest.raises(exc):
  1329. p.send_signal(signal.SIGTERM)
  1330. # test all methods
  1331. ns = process_namespace(p)
  1332. for fun, name in ns.iter(ns.getters + ns.setters):
  1333. try:
  1334. ret = fun()
  1335. except psutil.AccessDenied:
  1336. pass
  1337. else:
  1338. if name in {"uids", "gids"}:
  1339. assert ret.real == 0
  1340. elif name == "username":
  1341. user = 'NT AUTHORITY\\SYSTEM' if WINDOWS else 'root'
  1342. assert p.username() == user
  1343. elif name == "name":
  1344. assert name, name
  1345. if not OPENBSD:
  1346. assert 0 in psutil.pids()
  1347. assert psutil.pid_exists(0)
  1348. @pytest.mark.skipif(not HAS_ENVIRON, reason="not supported")
  1349. def test_environ(self):
  1350. def clean_dict(d):
  1351. exclude = {"PLAT", "HOME"}
  1352. if MACOS:
  1353. exclude.update([
  1354. "__CF_USER_TEXT_ENCODING",
  1355. "VERSIONER_PYTHON_PREFER_32_BIT",
  1356. "VERSIONER_PYTHON_VERSION",
  1357. ])
  1358. for name in list(d.keys()):
  1359. if name in exclude or name.startswith("PYTEST_"):
  1360. d.pop(name)
  1361. return {
  1362. k.replace("\r", "").replace("\n", ""): (
  1363. v.replace("\r", "").replace("\n", "")
  1364. )
  1365. for k, v in d.items()
  1366. }
  1367. self.maxDiff = None
  1368. p = psutil.Process()
  1369. d1 = clean_dict(p.environ())
  1370. d2 = clean_dict(os.environ.copy())
  1371. if not OSX and GITHUB_ACTIONS:
  1372. assert d1 == d2
  1373. @pytest.mark.skipif(not HAS_ENVIRON, reason="not supported")
  1374. @pytest.mark.skipif(not POSIX, reason="POSIX only")
  1375. @pytest.mark.skipif(
  1376. MACOS_11PLUS,
  1377. reason="macOS 11+ can't get another process environment, issue #2084",
  1378. )
  1379. @pytest.mark.skipif(
  1380. NETBSD, reason="sometimes fails on `assert is_running()`"
  1381. )
  1382. def test_weird_environ(self):
  1383. # environment variables can contain values without an equals sign
  1384. code = textwrap.dedent("""
  1385. #include <unistd.h>
  1386. #include <fcntl.h>
  1387. char * const argv[] = {"cat", 0};
  1388. char * const envp[] = {"A=1", "X", "C=3", 0};
  1389. int main(void) {
  1390. // Close stderr on exec so parent can wait for the
  1391. // execve to finish.
  1392. if (fcntl(2, F_SETFD, FD_CLOEXEC) != 0)
  1393. return 0;
  1394. return execve("/bin/cat", argv, envp);
  1395. }
  1396. """)
  1397. cexe = create_c_exe(self.get_testfn(), c_code=code)
  1398. sproc = self.spawn_subproc(
  1399. [cexe], stdin=subprocess.PIPE, stderr=subprocess.PIPE
  1400. )
  1401. p = psutil.Process(sproc.pid)
  1402. wait_for_pid(p.pid)
  1403. assert p.is_running()
  1404. # Wait for process to exec or exit.
  1405. assert sproc.stderr.read() == b""
  1406. if MACOS and CI_TESTING:
  1407. try:
  1408. env = p.environ()
  1409. except psutil.AccessDenied:
  1410. # XXX: fails sometimes with:
  1411. # PermissionError from 'sysctl(KERN_PROCARGS2) -> EIO'
  1412. return
  1413. else:
  1414. env = p.environ()
  1415. assert env == {"A": "1", "C": "3"}
  1416. sproc.communicate()
  1417. assert sproc.returncode == 0
  1418. # ===================================================================
  1419. # --- psutil.Popen tests
  1420. # ===================================================================
  1421. class TestPopen(PsutilTestCase):
  1422. """Tests for psutil.Popen class."""
  1423. @classmethod
  1424. def tearDownClass(cls):
  1425. reap_children()
  1426. @pytest.mark.skipif(MACOS and GITHUB_ACTIONS, reason="hangs on OSX + CI")
  1427. def test_misc(self):
  1428. # XXX this test causes a ResourceWarning because
  1429. # psutil.__subproc instance doesn't get properly freed.
  1430. # Not sure what to do though.
  1431. cmd = [
  1432. PYTHON_EXE,
  1433. "-c",
  1434. "import time; [time.sleep(0.1) for x in range(100)];",
  1435. ]
  1436. with psutil.Popen(
  1437. cmd,
  1438. stdout=subprocess.PIPE,
  1439. stderr=subprocess.PIPE,
  1440. env=PYTHON_EXE_ENV,
  1441. ) as proc:
  1442. proc.name()
  1443. proc.cpu_times()
  1444. proc.stdin # noqa: B018
  1445. assert dir(proc)
  1446. with pytest.raises(AttributeError):
  1447. proc.foo # noqa: B018
  1448. proc.terminate()
  1449. if POSIX:
  1450. assert proc.wait(5) == -signal.SIGTERM
  1451. else:
  1452. assert proc.wait(5) == signal.SIGTERM
  1453. def test_ctx_manager(self):
  1454. with psutil.Popen(
  1455. [PYTHON_EXE, "-V"],
  1456. stdout=subprocess.PIPE,
  1457. stderr=subprocess.PIPE,
  1458. stdin=subprocess.PIPE,
  1459. env=PYTHON_EXE_ENV,
  1460. ) as proc:
  1461. proc.communicate()
  1462. assert proc.stdout.closed
  1463. assert proc.stderr.closed
  1464. assert proc.stdin.closed
  1465. assert proc.returncode == 0
  1466. def test_kill_terminate(self):
  1467. # subprocess.Popen()'s terminate(), kill() and send_signal() do
  1468. # not raise exception after the process is gone. psutil.Popen
  1469. # diverges from that.
  1470. cmd = [
  1471. PYTHON_EXE,
  1472. "-c",
  1473. "import time; [time.sleep(0.1) for x in range(100)];",
  1474. ]
  1475. with psutil.Popen(
  1476. cmd,
  1477. stdout=subprocess.PIPE,
  1478. stderr=subprocess.PIPE,
  1479. env=PYTHON_EXE_ENV,
  1480. ) as proc:
  1481. proc.terminate()
  1482. proc.wait()
  1483. with pytest.raises(psutil.NoSuchProcess):
  1484. proc.terminate()
  1485. with pytest.raises(psutil.NoSuchProcess):
  1486. proc.kill()
  1487. with pytest.raises(psutil.NoSuchProcess):
  1488. proc.send_signal(signal.SIGTERM)
  1489. if WINDOWS:
  1490. with pytest.raises(psutil.NoSuchProcess):
  1491. proc.send_signal(signal.CTRL_C_EVENT)
  1492. with pytest.raises(psutil.NoSuchProcess):
  1493. proc.send_signal(signal.CTRL_BREAK_EVENT)
  1494. def test__getattribute__(self):
  1495. cmd = [
  1496. PYTHON_EXE,
  1497. "-c",
  1498. "import time; [time.sleep(0.1) for x in range(100)];",
  1499. ]
  1500. with psutil.Popen(
  1501. cmd,
  1502. stdout=subprocess.PIPE,
  1503. stderr=subprocess.PIPE,
  1504. env=PYTHON_EXE_ENV,
  1505. ) as proc:
  1506. proc.terminate()
  1507. proc.wait()
  1508. with pytest.raises(AttributeError):
  1509. proc.foo # noqa: B018