test_process.py 58 KB

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